﻿<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type='text/xsl' href="scriptsql.xsl"?>
<root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database includeDrop="true">ifc_nisis</database>
<procedures>
		
		<procedure owner="dbo" name="Ssh_COMUNI_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_COMUNI table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_COMUNE],
					[DS_COMUNE],
					[CD_PROVINCIA],
					[FL_FRAZIONE],
					[CD_COMUNE_RIF]
				FROM
					[dbo].[Ssh_COMUNI]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_COMUNI_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_COMUNI table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_COMUNE] nvarchar(10) COLLATE database_default  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([CD_COMUNE])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [CD_COMUNE]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_COMUNI]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[CD_COMUNE], O.[DS_COMUNE], O.[CD_PROVINCIA], O.[FL_FRAZIONE], O.[CD_COMUNE_RIF]
				FROM
				    [dbo].[Ssh_COMUNI] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[CD_COMUNE] = PageIndex.[CD_COMUNE]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_COMUNI]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_COMUNI_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_COMUNI table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@DsComune" type="nvarchar" direction="Input" size="60" precision="0" scale="0" param="(60)" nulldefault=""/>
				<parameter name="@CdProvincia" type="nvarchar" direction="Input" size="4" precision="0" scale="0" param="(4)" nulldefault=""/>
				<parameter name="@FlFrazione" type="nvarchar" direction="Input" size="1" precision="0" scale="0" param="(1)" nulldefault=""/>
				<parameter name="@CdComuneRif" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_COMUNI]
					(
					[CD_COMUNE]
					,[DS_COMUNE]
					,[CD_PROVINCIA]
					,[FL_FRAZIONE]
					,[CD_COMUNE_RIF]
					)
				VALUES
					(
					@CdComune
					,@DsComune
					,@CdProvincia
					,@FlFrazione
					,@CdComuneRif
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_COMUNI_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_COMUNI table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@OriginalCdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@DsComune" type="nvarchar" direction="Input" size="60" precision="0" scale="0" param="(60)" nulldefault=""/>
				<parameter name="@CdProvincia" type="nvarchar" direction="Input" size="4" precision="0" scale="0" param="(4)" nulldefault=""/>
				<parameter name="@FlFrazione" type="nvarchar" direction="Input" size="1" precision="0" scale="0" param="(1)" nulldefault=""/>
				<parameter name="@CdComuneRif" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_COMUNI]
				SET
					[CD_COMUNE] = @CdComune
					,[DS_COMUNE] = @DsComune
					,[CD_PROVINCIA] = @CdProvincia
					,[FL_FRAZIONE] = @FlFrazione
					,[CD_COMUNE_RIF] = @CdComuneRif
				WHERE
[CD_COMUNE] = @OriginalCdComune 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_COMUNI_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_COMUNI table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_COMUNI] WITH (ROWLOCK) 
				WHERE
					[CD_COMUNE] = @CdComune
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_COMUNI_GetByCdComune" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_COMUNI table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[CD_COMUNE],
					[DS_COMUNE],
					[CD_PROVINCIA],
					[FL_FRAZIONE],
					[CD_COMUNE_RIF]
				FROM
					[dbo].[Ssh_COMUNI]
				WHERE
					[CD_COMUNE] = @CdComune
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_COMUNI_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_COMUNI table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@DsComune" type="nvarchar" direction="Input" size="60" precision="0" scale="0" param="(60)" nulldefault="null"/>
				<parameter name="@CdProvincia" type="nvarchar" direction="Input" size="4" precision="0" scale="0" param="(4)" nulldefault="null"/>
				<parameter name="@FlFrazione" type="nvarchar" direction="Input" size="1" precision="0" scale="0" param="(1)" nulldefault="null"/>
				<parameter name="@CdComuneRif" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [CD_COMUNE]
	, [DS_COMUNE]
	, [CD_PROVINCIA]
	, [FL_FRAZIONE]
	, [CD_COMUNE_RIF]
    FROM
	[dbo].[Ssh_COMUNI]
    WHERE 
	 ([CD_COMUNE] = @CdComune OR @CdComune IS NULL)
	AND ([DS_COMUNE] = @DsComune OR @DsComune IS NULL)
	AND ([CD_PROVINCIA] = @CdProvincia OR @CdProvincia IS NULL)
	AND ([FL_FRAZIONE] = @FlFrazione OR @FlFrazione IS NULL)
	AND ([CD_COMUNE_RIF] = @CdComuneRif OR @CdComuneRif IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [CD_COMUNE]
	, [DS_COMUNE]
	, [CD_PROVINCIA]
	, [FL_FRAZIONE]
	, [CD_COMUNE_RIF]
    FROM
	[dbo].[Ssh_COMUNI]
    WHERE 
	 ([CD_COMUNE] = @CdComune AND @CdComune is not null)
	OR ([DS_COMUNE] = @DsComune AND @DsComune is not null)
	OR ([CD_PROVINCIA] = @CdProvincia AND @CdProvincia is not null)
	OR ([FL_FRAZIONE] = @FlFrazione AND @FlFrazione is not null)
	OR ([CD_COMUNE_RIF] = @CdComuneRif AND @CdComuneRif is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_COMUNI_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_COMUNI table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_COMUNE] nvarchar(10) COLLATE database_default  
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [CD_COMUNE] )
				SELECT TOP {3} [CD_COMUNE]
				FROM [dbo].[Ssh_COMUNI] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[CD_COMUNE], O.[DS_COMUNE], O.[CD_PROVINCIA], O.[FL_FRAZIONE], O.[CD_COMUNE_RIF]
				FROM
				    [dbo].[Ssh_COMUNI] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[CD_COMUNE] = PageIndex.[CD_COMUNE]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_COMUNI] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_Tipologia_Societa_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_Tipologia_Societa table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Id_Tipologia_Societa],
					[Dsc_Tipologia_Societa]
				FROM
					[dbo].[Ssh_Tipologia_Societa]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_Tipologia_Societa_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_Tipologia_Societa table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id_Tipologia_Societa] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id_Tipologia_Societa])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id_Tipologia_Societa]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_Tipologia_Societa]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id_Tipologia_Societa], O.[Dsc_Tipologia_Societa]
				FROM
				    [dbo].[Ssh_Tipologia_Societa] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id_Tipologia_Societa] = PageIndex.[Id_Tipologia_Societa]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_Tipologia_Societa]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_Tipologia_Societa_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_Tipologia_Societa table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@DscTipologiaSocieta" type="varchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_Tipologia_Societa]
					(
					[Id_Tipologia_Societa]
					,[Dsc_Tipologia_Societa]
					)
				VALUES
					(
					@IdTipologiaSocieta
					,@DscTipologiaSocieta
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Tipologia_Societa_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_Tipologia_Societa table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@OriginalIdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@DscTipologiaSocieta" type="varchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_Tipologia_Societa]
				SET
					[Id_Tipologia_Societa] = @IdTipologiaSocieta
					,[Dsc_Tipologia_Societa] = @DscTipologiaSocieta
				WHERE
[Id_Tipologia_Societa] = @OriginalIdTipologiaSocieta 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Tipologia_Societa_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_Tipologia_Societa table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_Tipologia_Societa] WITH (ROWLOCK) 
				WHERE
					[Id_Tipologia_Societa] = @IdTipologiaSocieta
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Tipologia_Societa_GetByIdTipologiaSocieta" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Tipologia_Societa table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[Id_Tipologia_Societa],
					[Dsc_Tipologia_Societa]
				FROM
					[dbo].[Ssh_Tipologia_Societa]
				WHERE
					[Id_Tipologia_Societa] = @IdTipologiaSocieta
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_Tipologia_Societa_GetByDscTipologiaSocieta" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Tipologia_Societa table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@DscTipologiaSocieta" type="varchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[Id_Tipologia_Societa],
					[Dsc_Tipologia_Societa]
				FROM
					[dbo].[Ssh_Tipologia_Societa]
				WHERE
					[Dsc_Tipologia_Societa] = @DscTipologiaSocieta
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_Tipologia_Societa_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_Tipologia_Societa table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@DscTipologiaSocieta" type="varchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id_Tipologia_Societa]
	, [Dsc_Tipologia_Societa]
    FROM
	[dbo].[Ssh_Tipologia_Societa]
    WHERE 
	 ([Id_Tipologia_Societa] = @IdTipologiaSocieta OR @IdTipologiaSocieta IS NULL)
	AND ([Dsc_Tipologia_Societa] = @DscTipologiaSocieta OR @DscTipologiaSocieta IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id_Tipologia_Societa]
	, [Dsc_Tipologia_Societa]
    FROM
	[dbo].[Ssh_Tipologia_Societa]
    WHERE 
	 ([Id_Tipologia_Societa] = @IdTipologiaSocieta AND @IdTipologiaSocieta is not null)
	OR ([Dsc_Tipologia_Societa] = @DscTipologiaSocieta AND @DscTipologiaSocieta is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_Tipologia_Societa_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_Tipologia_Societa table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id_Tipologia_Societa] int 
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [Id_Tipologia_Societa] )
				SELECT TOP {3} [Id_Tipologia_Societa]
				FROM [dbo].[Ssh_Tipologia_Societa] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[Id_Tipologia_Societa], O.[Dsc_Tipologia_Societa]
				FROM
				    [dbo].[Ssh_Tipologia_Societa] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[Id_Tipologia_Societa] = PageIndex.[Id_Tipologia_Societa]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_Tipologia_Societa] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_Trader_Prec_Storico_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_Trader_Prec_Storico table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Id_Trader_Prec_Storico],
					[mese_inizio_fornitura],
					[piva],
					[ragione_sociale],
					[id_tipologia],
					[Id_Batch_Relazioni],
					[cd_Trader_Precedente]
				FROM
					[dbo].[Ssh_Trader_Prec_Storico]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_Trader_Prec_Storico_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_Trader_Prec_Storico table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id_Trader_Prec_Storico] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id_Trader_Prec_Storico])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id_Trader_Prec_Storico]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_Trader_Prec_Storico]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id_Trader_Prec_Storico], O.[mese_inizio_fornitura], O.[piva], O.[ragione_sociale], O.[id_tipologia], O.[Id_Batch_Relazioni], O.[cd_Trader_Precedente]
				FROM
				    [dbo].[Ssh_Trader_Prec_Storico] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id_Trader_Prec_Storico] = PageIndex.[Id_Trader_Prec_Storico]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_Trader_Prec_Storico]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_Trader_Prec_Storico_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_Trader_Prec_Storico table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecStorico" type="int" direction="Output" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@MeseInizioFornitura" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@Piva" type="nvarchar" direction="Input" size="30" precision="0" scale="0" param="(30)" nulldefault=""/>
				<parameter name="@RagioneSociale" type="nvarchar" direction="Input" size="100" precision="0" scale="0" param="(100)" nulldefault=""/>
				<parameter name="@IdTipologia" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@IdBatchRelazioni" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@CdTraderPrecedente" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_Trader_Prec_Storico]
					(
					[mese_inizio_fornitura]
					,[piva]
					,[ragione_sociale]
					,[id_tipologia]
					,[Id_Batch_Relazioni]
					,[cd_Trader_Precedente]
					)
				VALUES
					(
					@MeseInizioFornitura
					,@Piva
					,@RagioneSociale
					,@IdTipologia
					,@IdBatchRelazioni
					,@CdTraderPrecedente
					)
				
				-- Get the identity value
				SET @IdTraderPrecStorico = SCOPE_IDENTITY()
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Trader_Prec_Storico_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_Trader_Prec_Storico table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecStorico" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@MeseInizioFornitura" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@Piva" type="nvarchar" direction="Input" size="30" precision="0" scale="0" param="(30)" nulldefault=""/>
				<parameter name="@RagioneSociale" type="nvarchar" direction="Input" size="100" precision="0" scale="0" param="(100)" nulldefault=""/>
				<parameter name="@IdTipologia" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@IdBatchRelazioni" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@CdTraderPrecedente" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_Trader_Prec_Storico]
				SET
					[mese_inizio_fornitura] = @MeseInizioFornitura
					,[piva] = @Piva
					,[ragione_sociale] = @RagioneSociale
					,[id_tipologia] = @IdTipologia
					,[Id_Batch_Relazioni] = @IdBatchRelazioni
					,[cd_Trader_Precedente] = @CdTraderPrecedente
				WHERE
[Id_Trader_Prec_Storico] = @IdTraderPrecStorico 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Trader_Prec_Storico_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_Trader_Prec_Storico table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecStorico" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_Trader_Prec_Storico] WITH (ROWLOCK) 
				WHERE
					[Id_Trader_Prec_Storico] = @IdTraderPrecStorico
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Trader_Prec_Storico_GetByIdTipologia" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Trader_Prec_Storico table through a foreign key
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTipologia" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SET ANSI_NULLS OFF
				
				SELECT
					[Id_Trader_Prec_Storico],
					[mese_inizio_fornitura],
					[piva],
					[ragione_sociale],
					[id_tipologia],
					[Id_Batch_Relazioni],
					[cd_Trader_Precedente]
				FROM
					[dbo].[Ssh_Trader_Prec_Storico]
				WHERE
					[id_tipologia] = @IdTipologia
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Trader_Prec_Storico_GetByIdBatchRelazioni" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Trader_Prec_Storico table through a foreign key
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdBatchRelazioni" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SET ANSI_NULLS OFF
				
				SELECT
					[Id_Trader_Prec_Storico],
					[mese_inizio_fornitura],
					[piva],
					[ragione_sociale],
					[id_tipologia],
					[Id_Batch_Relazioni],
					[cd_Trader_Precedente]
				FROM
					[dbo].[Ssh_Trader_Prec_Storico]
				WHERE
					[Id_Batch_Relazioni] = @IdBatchRelazioni
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Trader_Prec_Storico_GetByIdTraderPrecStorico" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Trader_Prec_Storico table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecStorico" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[Id_Trader_Prec_Storico],
					[mese_inizio_fornitura],
					[piva],
					[ragione_sociale],
					[id_tipologia],
					[Id_Batch_Relazioni],
					[cd_Trader_Precedente]
				FROM
					[dbo].[Ssh_Trader_Prec_Storico]
				WHERE
					[Id_Trader_Prec_Storico] = @IdTraderPrecStorico
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_Trader_Prec_Storico_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_Trader_Prec_Storico table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdTraderPrecStorico" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@MeseInizioFornitura" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
				<parameter name="@Piva" type="nvarchar" direction="Input" size="30" precision="0" scale="0" param="(30)" nulldefault="null"/>
				<parameter name="@RagioneSociale" type="nvarchar" direction="Input" size="100" precision="0" scale="0" param="(100)" nulldefault="null"/>
				<parameter name="@IdTipologia" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdBatchRelazioni" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdTraderPrecedente" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id_Trader_Prec_Storico]
	, [mese_inizio_fornitura]
	, [piva]
	, [ragione_sociale]
	, [id_tipologia]
	, [Id_Batch_Relazioni]
	, [cd_Trader_Precedente]
    FROM
	[dbo].[Ssh_Trader_Prec_Storico]
    WHERE 
	 ([Id_Trader_Prec_Storico] = @IdTraderPrecStorico OR @IdTraderPrecStorico IS NULL)
	AND ([mese_inizio_fornitura] = @MeseInizioFornitura OR @MeseInizioFornitura IS NULL)
	AND ([piva] = @Piva OR @Piva IS NULL)
	AND ([ragione_sociale] = @RagioneSociale OR @RagioneSociale IS NULL)
	AND ([id_tipologia] = @IdTipologia OR @IdTipologia IS NULL)
	AND ([Id_Batch_Relazioni] = @IdBatchRelazioni OR @IdBatchRelazioni IS NULL)
	AND ([cd_Trader_Precedente] = @CdTraderPrecedente OR @CdTraderPrecedente IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id_Trader_Prec_Storico]
	, [mese_inizio_fornitura]
	, [piva]
	, [ragione_sociale]
	, [id_tipologia]
	, [Id_Batch_Relazioni]
	, [cd_Trader_Precedente]
    FROM
	[dbo].[Ssh_Trader_Prec_Storico]
    WHERE 
	 ([Id_Trader_Prec_Storico] = @IdTraderPrecStorico AND @IdTraderPrecStorico is not null)
	OR ([mese_inizio_fornitura] = @MeseInizioFornitura AND @MeseInizioFornitura is not null)
	OR ([piva] = @Piva AND @Piva is not null)
	OR ([ragione_sociale] = @RagioneSociale AND @RagioneSociale is not null)
	OR ([id_tipologia] = @IdTipologia AND @IdTipologia is not null)
	OR ([Id_Batch_Relazioni] = @IdBatchRelazioni AND @IdBatchRelazioni is not null)
	OR ([cd_Trader_Precedente] = @CdTraderPrecedente AND @CdTraderPrecedente is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_Trader_Prec_Storico_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_Trader_Prec_Storico table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id_Trader_Prec_Storico] int 
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [Id_Trader_Prec_Storico] )
				SELECT TOP {3} [Id_Trader_Prec_Storico]
				FROM [dbo].[Ssh_Trader_Prec_Storico] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[Id_Trader_Prec_Storico], O.[mese_inizio_fornitura], O.[piva], O.[ragione_sociale], O.[id_tipologia], O.[Id_Batch_Relazioni], O.[cd_Trader_Precedente]
				FROM
				    [dbo].[Ssh_Trader_Prec_Storico] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[Id_Trader_Prec_Storico] = PageIndex.[Id_Trader_Prec_Storico]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_Trader_Prec_Storico] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_PERIODI_FORNITURA_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_PERIODI_FORNITURA table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_MESE_INIZIO_FORNITURA],
					[DS_MESE_INIZIO_FORNITURA]
				FROM
					[dbo].[Ssh_PERIODI_FORNITURA]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_PERIODI_FORNITURA_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_PERIODI_FORNITURA table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_MESE_INIZIO_FORNITURA] nvarchar(50) COLLATE database_default  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([CD_MESE_INIZIO_FORNITURA])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [CD_MESE_INIZIO_FORNITURA]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_PERIODI_FORNITURA]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[CD_MESE_INIZIO_FORNITURA], O.[DS_MESE_INIZIO_FORNITURA]
				FROM
				    [dbo].[Ssh_PERIODI_FORNITURA] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[CD_MESE_INIZIO_FORNITURA] = PageIndex.[CD_MESE_INIZIO_FORNITURA]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_PERIODI_FORNITURA]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_PERIODI_FORNITURA_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_PERIODI_FORNITURA table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdMeseInizioFornitura" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@DsMeseInizioFornitura" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_PERIODI_FORNITURA]
					(
					[CD_MESE_INIZIO_FORNITURA]
					,[DS_MESE_INIZIO_FORNITURA]
					)
				VALUES
					(
					@CdMeseInizioFornitura
					,@DsMeseInizioFornitura
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_PERIODI_FORNITURA_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_PERIODI_FORNITURA table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdMeseInizioFornitura" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@OriginalCdMeseInizioFornitura" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@DsMeseInizioFornitura" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_PERIODI_FORNITURA]
				SET
					[CD_MESE_INIZIO_FORNITURA] = @CdMeseInizioFornitura
					,[DS_MESE_INIZIO_FORNITURA] = @DsMeseInizioFornitura
				WHERE
[CD_MESE_INIZIO_FORNITURA] = @OriginalCdMeseInizioFornitura 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_PERIODI_FORNITURA_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_PERIODI_FORNITURA table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdMeseInizioFornitura" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_PERIODI_FORNITURA] WITH (ROWLOCK) 
				WHERE
					[CD_MESE_INIZIO_FORNITURA] = @CdMeseInizioFornitura
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_PERIODI_FORNITURA_GetByCdMeseInizioFornitura" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_PERIODI_FORNITURA table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdMeseInizioFornitura" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[CD_MESE_INIZIO_FORNITURA],
					[DS_MESE_INIZIO_FORNITURA]
				FROM
					[dbo].[Ssh_PERIODI_FORNITURA]
				WHERE
					[CD_MESE_INIZIO_FORNITURA] = @CdMeseInizioFornitura
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_PERIODI_FORNITURA_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_PERIODI_FORNITURA table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdMeseInizioFornitura" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
				<parameter name="@DsMeseInizioFornitura" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [CD_MESE_INIZIO_FORNITURA]
	, [DS_MESE_INIZIO_FORNITURA]
    FROM
	[dbo].[Ssh_PERIODI_FORNITURA]
    WHERE 
	 ([CD_MESE_INIZIO_FORNITURA] = @CdMeseInizioFornitura OR @CdMeseInizioFornitura IS NULL)
	AND ([DS_MESE_INIZIO_FORNITURA] = @DsMeseInizioFornitura OR @DsMeseInizioFornitura IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [CD_MESE_INIZIO_FORNITURA]
	, [DS_MESE_INIZIO_FORNITURA]
    FROM
	[dbo].[Ssh_PERIODI_FORNITURA]
    WHERE 
	 ([CD_MESE_INIZIO_FORNITURA] = @CdMeseInizioFornitura AND @CdMeseInizioFornitura is not null)
	OR ([DS_MESE_INIZIO_FORNITURA] = @DsMeseInizioFornitura AND @DsMeseInizioFornitura is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_PERIODI_FORNITURA_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_PERIODI_FORNITURA table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_MESE_INIZIO_FORNITURA] nvarchar(50) COLLATE database_default  
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [CD_MESE_INIZIO_FORNITURA] )
				SELECT TOP {3} [CD_MESE_INIZIO_FORNITURA]
				FROM [dbo].[Ssh_PERIODI_FORNITURA] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[CD_MESE_INIZIO_FORNITURA], O.[DS_MESE_INIZIO_FORNITURA]
				FROM
				    [dbo].[Ssh_PERIODI_FORNITURA] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[CD_MESE_INIZIO_FORNITURA] = PageIndex.[CD_MESE_INIZIO_FORNITURA]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_PERIODI_FORNITURA] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_RELAZIONI_SOCIETARIE_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_RELAZIONI_SOCIETARIE table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_STORICO_MENSILE_RETE],
					[CD_POOL],
					[CD_SOCIETA1],
					[CD_SOCIETA2],
					[TIPOLOGIA_RELAZIONE]
				FROM
					[dbo].[Ssh_RELAZIONI_SOCIETARIE]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_RELAZIONI_SOCIETARIE_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_RELAZIONI_SOCIETARIE table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_STORICO_MENSILE_RETE] nvarchar(6) COLLATE database_default , [CD_POOL] nvarchar(20) COLLATE database_default , [CD_SOCIETA1] nvarchar(10) COLLATE database_default , [CD_SOCIETA2] nvarchar(50) COLLATE database_default  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([CD_STORICO_MENSILE_RETE], [CD_POOL], [CD_SOCIETA1], [CD_SOCIETA2])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [CD_STORICO_MENSILE_RETE], [CD_POOL], [CD_SOCIETA1], [CD_SOCIETA2]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_RELAZIONI_SOCIETARIE]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[CD_STORICO_MENSILE_RETE], O.[CD_POOL], O.[CD_SOCIETA1], O.[CD_SOCIETA2], O.[TIPOLOGIA_RELAZIONE]
				FROM
				    [dbo].[Ssh_RELAZIONI_SOCIETARIE] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[CD_STORICO_MENSILE_RETE] = PageIndex.[CD_STORICO_MENSILE_RETE]
					AND O.[CD_POOL] = PageIndex.[CD_POOL]
					AND O.[CD_SOCIETA1] = PageIndex.[CD_SOCIETA1]
					AND O.[CD_SOCIETA2] = PageIndex.[CD_SOCIETA2]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_RELAZIONI_SOCIETARIE]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_RELAZIONI_SOCIETARIE_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_RELAZIONI_SOCIETARIE table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdSocieta1" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdSocieta2" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@TipologiaRelazione" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_RELAZIONI_SOCIETARIE]
					(
					[CD_STORICO_MENSILE_RETE]
					,[CD_POOL]
					,[CD_SOCIETA1]
					,[CD_SOCIETA2]
					,[TIPOLOGIA_RELAZIONE]
					)
				VALUES
					(
					@CdStoricoMensileRete
					,@CdPool
					,@CdSocieta1
					,@CdSocieta2
					,@TipologiaRelazione
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_RELAZIONI_SOCIETARIE_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_RELAZIONI_SOCIETARIE table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@OriginalCdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@OriginalCdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdSocieta1" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@OriginalCdSocieta1" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdSocieta2" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@OriginalCdSocieta2" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@TipologiaRelazione" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_RELAZIONI_SOCIETARIE]
				SET
					[CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete
					,[CD_POOL] = @CdPool
					,[CD_SOCIETA1] = @CdSocieta1
					,[CD_SOCIETA2] = @CdSocieta2
					,[TIPOLOGIA_RELAZIONE] = @TipologiaRelazione
				WHERE
[CD_STORICO_MENSILE_RETE] = @OriginalCdStoricoMensileRete 
AND [CD_POOL] = @OriginalCdPool 
AND [CD_SOCIETA1] = @OriginalCdSocieta1 
AND [CD_SOCIETA2] = @OriginalCdSocieta2 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_RELAZIONI_SOCIETARIE_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_RELAZIONI_SOCIETARIE table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdSocieta1" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdSocieta2" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_RELAZIONI_SOCIETARIE] WITH (ROWLOCK) 
				WHERE
					[CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete
					AND [CD_POOL] = @CdPool
					AND [CD_SOCIETA1] = @CdSocieta1
					AND [CD_SOCIETA2] = @CdSocieta2
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_RELAZIONI_SOCIETARIE_GetByCdStoricoMensileReteCdPoolCdSocieta1CdSocieta2" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_RELAZIONI_SOCIETARIE table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdSocieta1" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdSocieta2" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[CD_STORICO_MENSILE_RETE],
					[CD_POOL],
					[CD_SOCIETA1],
					[CD_SOCIETA2],
					[TIPOLOGIA_RELAZIONE]
				FROM
					[dbo].[Ssh_RELAZIONI_SOCIETARIE]
				WHERE
					[CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete
					AND [CD_POOL] = @CdPool
					AND [CD_SOCIETA1] = @CdSocieta1
					AND [CD_SOCIETA2] = @CdSocieta2
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_RELAZIONI_SOCIETARIE_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_RELAZIONI_SOCIETARIE table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@CdSocieta1" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@CdSocieta2" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
				<parameter name="@TipologiaRelazione" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [CD_STORICO_MENSILE_RETE]
	, [CD_POOL]
	, [CD_SOCIETA1]
	, [CD_SOCIETA2]
	, [TIPOLOGIA_RELAZIONE]
    FROM
	[dbo].[Ssh_RELAZIONI_SOCIETARIE]
    WHERE 
	 ([CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete OR @CdStoricoMensileRete IS NULL)
	AND ([CD_POOL] = @CdPool OR @CdPool IS NULL)
	AND ([CD_SOCIETA1] = @CdSocieta1 OR @CdSocieta1 IS NULL)
	AND ([CD_SOCIETA2] = @CdSocieta2 OR @CdSocieta2 IS NULL)
	AND ([TIPOLOGIA_RELAZIONE] = @TipologiaRelazione OR @TipologiaRelazione IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [CD_STORICO_MENSILE_RETE]
	, [CD_POOL]
	, [CD_SOCIETA1]
	, [CD_SOCIETA2]
	, [TIPOLOGIA_RELAZIONE]
    FROM
	[dbo].[Ssh_RELAZIONI_SOCIETARIE]
    WHERE 
	 ([CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete AND @CdStoricoMensileRete is not null)
	OR ([CD_POOL] = @CdPool AND @CdPool is not null)
	OR ([CD_SOCIETA1] = @CdSocieta1 AND @CdSocieta1 is not null)
	OR ([CD_SOCIETA2] = @CdSocieta2 AND @CdSocieta2 is not null)
	OR ([TIPOLOGIA_RELAZIONE] = @TipologiaRelazione AND @TipologiaRelazione is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_RELAZIONI_SOCIETARIE_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_RELAZIONI_SOCIETARIE table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_STORICO_MENSILE_RETE] nvarchar(6) COLLATE database_default , [CD_POOL] nvarchar(20) COLLATE database_default , [CD_SOCIETA1] nvarchar(10) COLLATE database_default , [CD_SOCIETA2] nvarchar(50) COLLATE database_default  
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [CD_STORICO_MENSILE_RETE], [CD_POOL], [CD_SOCIETA1], [CD_SOCIETA2] )
				SELECT TOP {3} [CD_STORICO_MENSILE_RETE], [CD_POOL], [CD_SOCIETA1], [CD_SOCIETA2]
				FROM [dbo].[Ssh_RELAZIONI_SOCIETARIE] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[CD_STORICO_MENSILE_RETE], O.[CD_POOL], O.[CD_SOCIETA1], O.[CD_SOCIETA2], O.[TIPOLOGIA_RELAZIONE]
				FROM
				    [dbo].[Ssh_RELAZIONI_SOCIETARIE] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[CD_STORICO_MENSILE_RETE] = PageIndex.[CD_STORICO_MENSILE_RETE]
				    AND O.[CD_POOL] = PageIndex.[CD_POOL]
				    AND O.[CD_SOCIETA1] = PageIndex.[CD_SOCIETA1]
				    AND O.[CD_SOCIETA2] = PageIndex.[CD_SOCIETA2]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_RELAZIONI_SOCIETARIE] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_Batch_Relazioni_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_Batch_Relazioni table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Id_Batch],
					[Cd_Storico_Mensile_Rete],
					[dt_ins],
					[dt_upd],
					[Cd_Pool]
				FROM
					[dbo].[Ssh_Batch_Relazioni]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_Batch_Relazioni_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_Batch_Relazioni table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id_Batch] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id_Batch])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id_Batch]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_Batch_Relazioni]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id_Batch], O.[Cd_Storico_Mensile_Rete], O.[dt_ins], O.[dt_upd], O.[Cd_Pool]
				FROM
				    [dbo].[Ssh_Batch_Relazioni] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id_Batch] = PageIndex.[Id_Batch]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_Batch_Relazioni]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_Batch_Relazioni_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_Batch_Relazioni table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Output" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@DtIns" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault=""/>
				<parameter name="@DtUpd" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_Batch_Relazioni]
					(
					[Cd_Storico_Mensile_Rete]
					,[dt_ins]
					,[dt_upd]
					,[Cd_Pool]
					)
				VALUES
					(
					@CdStoricoMensileRete
					,@DtIns
					,@DtUpd
					,@CdPool
					)
				
				-- Get the identity value
				SET @IdBatch = SCOPE_IDENTITY()
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Batch_Relazioni_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_Batch_Relazioni table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@DtIns" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault=""/>
				<parameter name="@DtUpd" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_Batch_Relazioni]
				SET
					[Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete
					,[dt_ins] = @DtIns
					,[dt_upd] = @DtUpd
					,[Cd_Pool] = @CdPool
				WHERE
[Id_Batch] = @IdBatch 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Batch_Relazioni_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_Batch_Relazioni table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_Batch_Relazioni] WITH (ROWLOCK) 
				WHERE
					[Id_Batch] = @IdBatch
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Batch_Relazioni_GetByIdBatch" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Batch_Relazioni table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[Id_Batch],
					[Cd_Storico_Mensile_Rete],
					[dt_ins],
					[dt_upd],
					[Cd_Pool]
				FROM
					[dbo].[Ssh_Batch_Relazioni]
				WHERE
					[Id_Batch] = @IdBatch
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_Batch_Relazioni_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_Batch_Relazioni table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@DtIns" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault="null"/>
				<parameter name="@DtUpd" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault="null"/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id_Batch]
	, [Cd_Storico_Mensile_Rete]
	, [dt_ins]
	, [dt_upd]
	, [Cd_Pool]
    FROM
	[dbo].[Ssh_Batch_Relazioni]
    WHERE 
	 ([Id_Batch] = @IdBatch OR @IdBatch IS NULL)
	AND ([Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete OR @CdStoricoMensileRete IS NULL)
	AND ([dt_ins] = @DtIns OR @DtIns IS NULL)
	AND ([dt_upd] = @DtUpd OR @DtUpd IS NULL)
	AND ([Cd_Pool] = @CdPool OR @CdPool IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id_Batch]
	, [Cd_Storico_Mensile_Rete]
	, [dt_ins]
	, [dt_upd]
	, [Cd_Pool]
    FROM
	[dbo].[Ssh_Batch_Relazioni]
    WHERE 
	 ([Id_Batch] = @IdBatch AND @IdBatch is not null)
	OR ([Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete AND @CdStoricoMensileRete is not null)
	OR ([dt_ins] = @DtIns AND @DtIns is not null)
	OR ([dt_upd] = @DtUpd AND @DtUpd is not null)
	OR ([Cd_Pool] = @CdPool AND @CdPool is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_Batch_Relazioni_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_Batch_Relazioni table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id_Batch] int 
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [Id_Batch] )
				SELECT TOP {3} [Id_Batch]
				FROM [dbo].[Ssh_Batch_Relazioni] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[Id_Batch], O.[Cd_Storico_Mensile_Rete], O.[dt_ins], O.[dt_upd], O.[Cd_Pool]
				FROM
				    [dbo].[Ssh_Batch_Relazioni] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[Id_Batch] = PageIndex.[Id_Batch]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_Batch_Relazioni] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_PoolState_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_PoolState table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Cd_Pool],
					[Id_Stato_Relazione],
					[Cd_Storico_Mensile_Rete],
					[Cd_Comune]
				FROM
					[dbo].[Ssh_PoolState]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_PoolState_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_PoolState table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Cd_Pool] nvarchar(20) COLLATE database_default , [Cd_Storico_Mensile_Rete] nvarchar(6) COLLATE database_default , [Cd_Comune] nvarchar(50) COLLATE database_default  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Cd_Pool], [Cd_Storico_Mensile_Rete], [Cd_Comune])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Cd_Pool], [Cd_Storico_Mensile_Rete], [Cd_Comune]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_PoolState]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Cd_Pool], O.[Id_Stato_Relazione], O.[Cd_Storico_Mensile_Rete], O.[Cd_Comune]
				FROM
				    [dbo].[Ssh_PoolState] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Cd_Pool] = PageIndex.[Cd_Pool]
					AND O.[Cd_Storico_Mensile_Rete] = PageIndex.[Cd_Storico_Mensile_Rete]
					AND O.[Cd_Comune] = PageIndex.[Cd_Comune]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_PoolState]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_PoolState_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_PoolState table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@IdStatoRelazione" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_PoolState]
					(
					[Cd_Pool]
					,[Id_Stato_Relazione]
					,[Cd_Storico_Mensile_Rete]
					,[Cd_Comune]
					)
				VALUES
					(
					@CdPool
					,@IdStatoRelazione
					,@CdStoricoMensileRete
					,@CdComune
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_PoolState_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_PoolState table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@OriginalCdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@IdStatoRelazione" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@OriginalCdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@OriginalCdComune" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_PoolState]
				SET
					[Cd_Pool] = @CdPool
					,[Id_Stato_Relazione] = @IdStatoRelazione
					,[Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete
					,[Cd_Comune] = @CdComune
				WHERE
[Cd_Pool] = @OriginalCdPool 
AND [Cd_Storico_Mensile_Rete] = @OriginalCdStoricoMensileRete 
AND [Cd_Comune] = @OriginalCdComune 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_PoolState_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_PoolState table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_PoolState] WITH (ROWLOCK) 
				WHERE
					[Cd_Pool] = @CdPool
					AND [Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete
					AND [Cd_Comune] = @CdComune
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_PoolState_GetByIdStatoRelazione" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_PoolState table through a foreign key
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdStatoRelazione" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SET ANSI_NULLS OFF
				
				SELECT
					[Cd_Pool],
					[Id_Stato_Relazione],
					[Cd_Storico_Mensile_Rete],
					[Cd_Comune]
				FROM
					[dbo].[Ssh_PoolState]
				WHERE
					[Id_Stato_Relazione] = @IdStatoRelazione
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_PoolState_GetByCdPoolCdStoricoMensileReteCdComune" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_PoolState table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[Cd_Pool],
					[Id_Stato_Relazione],
					[Cd_Storico_Mensile_Rete],
					[Cd_Comune]
				FROM
					[dbo].[Ssh_PoolState]
				WHERE
					[Cd_Pool] = @CdPool
					AND [Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete
					AND [Cd_Comune] = @CdComune
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_PoolState_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_PoolState table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@IdStatoRelazione" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Cd_Pool]
	, [Id_Stato_Relazione]
	, [Cd_Storico_Mensile_Rete]
	, [Cd_Comune]
    FROM
	[dbo].[Ssh_PoolState]
    WHERE 
	 ([Cd_Pool] = @CdPool OR @CdPool IS NULL)
	AND ([Id_Stato_Relazione] = @IdStatoRelazione OR @IdStatoRelazione IS NULL)
	AND ([Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete OR @CdStoricoMensileRete IS NULL)
	AND ([Cd_Comune] = @CdComune OR @CdComune IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Cd_Pool]
	, [Id_Stato_Relazione]
	, [Cd_Storico_Mensile_Rete]
	, [Cd_Comune]
    FROM
	[dbo].[Ssh_PoolState]
    WHERE 
	 ([Cd_Pool] = @CdPool AND @CdPool is not null)
	OR ([Id_Stato_Relazione] = @IdStatoRelazione AND @IdStatoRelazione is not null)
	OR ([Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete AND @CdStoricoMensileRete is not null)
	OR ([Cd_Comune] = @CdComune AND @CdComune is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_PoolState_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_PoolState table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Cd_Pool] nvarchar(20) COLLATE database_default , [Cd_Storico_Mensile_Rete] nvarchar(6) COLLATE database_default , [Cd_Comune] nvarchar(50) COLLATE database_default  
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [Cd_Pool], [Cd_Storico_Mensile_Rete], [Cd_Comune] )
				SELECT TOP {3} [Cd_Pool], [Cd_Storico_Mensile_Rete], [Cd_Comune]
				FROM [dbo].[Ssh_PoolState] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[Cd_Pool], O.[Id_Stato_Relazione], O.[Cd_Storico_Mensile_Rete], O.[Cd_Comune]
				FROM
				    [dbo].[Ssh_PoolState] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[Cd_Pool] = PageIndex.[Cd_Pool]
				    AND O.[Cd_Storico_Mensile_Rete] = PageIndex.[Cd_Storico_Mensile_Rete]
				    AND O.[Cd_Comune] = PageIndex.[Cd_Comune]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_PoolState] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_Utenze_Gas_Storico_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_Utenze_Gas_Storico table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Id_UtenzeGas_Storico],
					[Id_Trader_Prec_Storico],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[CD_ATECO],
					[CD_PUNTO_FORNITURA],
					[CD_COMUNE],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[Id_Stato],
					[Note],
					[s_area_uscita]
				FROM
					[dbo].[Ssh_Utenze_Gas_Storico]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_Utenze_Gas_Storico_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_Utenze_Gas_Storico table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id_UtenzeGas_Storico] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id_UtenzeGas_Storico])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id_UtenzeGas_Storico]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_Utenze_Gas_Storico]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id_UtenzeGas_Storico], O.[Id_Trader_Prec_Storico], O.[CD_CLIENTE], O.[DS_CLIENTE], O.[CD_ATECO], O.[CD_PUNTO_FORNITURA], O.[CD_COMUNE], O.[CONSUMO_ANNUO_STIMATO], O.[CD_PRELIEVO_AEEG], O.[Id_Stato], O.[Note], O.[s_area_uscita]
				FROM
				    [dbo].[Ssh_Utenze_Gas_Storico] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id_UtenzeGas_Storico] = PageIndex.[Id_UtenzeGas_Storico]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_Utenze_Gas_Storico]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_Utenze_Gas_Storico_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_Utenze_Gas_Storico table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdUtenzeGasStorico" type="int" direction="Output" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@IdTraderPrecStorico" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@CdCliente" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@DsCliente" type="nvarchar" direction="Input" size="60" precision="0" scale="0" param="(60)" nulldefault=""/>
				<parameter name="@CdAteco" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdPuntoFornitura" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@ConsumoAnnuoStimato" type="nvarchar" direction="Input" size="38" precision="0" scale="0" param="(38)" nulldefault=""/>
				<parameter name="@CdPrelievoAeeg" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@IdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@Note" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault=""/>
				<parameter name="@SAreaUscita" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_Utenze_Gas_Storico]
					(
					[Id_Trader_Prec_Storico]
					,[CD_CLIENTE]
					,[DS_CLIENTE]
					,[CD_ATECO]
					,[CD_PUNTO_FORNITURA]
					,[CD_COMUNE]
					,[CONSUMO_ANNUO_STIMATO]
					,[CD_PRELIEVO_AEEG]
					,[Id_Stato]
					,[Note]
					,[s_area_uscita]
					)
				VALUES
					(
					@IdTraderPrecStorico
					,@CdCliente
					,@DsCliente
					,@CdAteco
					,@CdPuntoFornitura
					,@CdComune
					,@ConsumoAnnuoStimato
					,@CdPrelievoAeeg
					,@IdStato
					,@Note
					,@SAreaUscita
					)
				
				-- Get the identity value
				SET @IdUtenzeGasStorico = SCOPE_IDENTITY()
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Utenze_Gas_Storico_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_Utenze_Gas_Storico table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdUtenzeGasStorico" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@IdTraderPrecStorico" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@CdCliente" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@DsCliente" type="nvarchar" direction="Input" size="60" precision="0" scale="0" param="(60)" nulldefault=""/>
				<parameter name="@CdAteco" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdPuntoFornitura" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@ConsumoAnnuoStimato" type="nvarchar" direction="Input" size="38" precision="0" scale="0" param="(38)" nulldefault=""/>
				<parameter name="@CdPrelievoAeeg" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@IdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@Note" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault=""/>
				<parameter name="@SAreaUscita" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_Utenze_Gas_Storico]
				SET
					[Id_Trader_Prec_Storico] = @IdTraderPrecStorico
					,[CD_CLIENTE] = @CdCliente
					,[DS_CLIENTE] = @DsCliente
					,[CD_ATECO] = @CdAteco
					,[CD_PUNTO_FORNITURA] = @CdPuntoFornitura
					,[CD_COMUNE] = @CdComune
					,[CONSUMO_ANNUO_STIMATO] = @ConsumoAnnuoStimato
					,[CD_PRELIEVO_AEEG] = @CdPrelievoAeeg
					,[Id_Stato] = @IdStato
					,[Note] = @Note
					,[s_area_uscita] = @SAreaUscita
				WHERE
[Id_UtenzeGas_Storico] = @IdUtenzeGasStorico 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Utenze_Gas_Storico_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_Utenze_Gas_Storico table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdUtenzeGasStorico" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_Utenze_Gas_Storico] WITH (ROWLOCK) 
				WHERE
					[Id_UtenzeGas_Storico] = @IdUtenzeGasStorico
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Utenze_Gas_Storico_GetByIdStato" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Utenze_Gas_Storico table through a foreign key
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SET ANSI_NULLS OFF
				
				SELECT
					[Id_UtenzeGas_Storico],
					[Id_Trader_Prec_Storico],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[CD_ATECO],
					[CD_PUNTO_FORNITURA],
					[CD_COMUNE],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[Id_Stato],
					[Note],
					[s_area_uscita]
				FROM
					[dbo].[Ssh_Utenze_Gas_Storico]
				WHERE
					[Id_Stato] = @IdStato
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Utenze_Gas_Storico_GetByIdTraderPrecStorico" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Utenze_Gas_Storico table through a foreign key
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecStorico" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SET ANSI_NULLS OFF
				
				SELECT
					[Id_UtenzeGas_Storico],
					[Id_Trader_Prec_Storico],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[CD_ATECO],
					[CD_PUNTO_FORNITURA],
					[CD_COMUNE],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[Id_Stato],
					[Note],
					[s_area_uscita]
				FROM
					[dbo].[Ssh_Utenze_Gas_Storico]
				WHERE
					[Id_Trader_Prec_Storico] = @IdTraderPrecStorico
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Utenze_Gas_Storico_GetByIdUtenzeGasStorico" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Utenze_Gas_Storico table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdUtenzeGasStorico" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[Id_UtenzeGas_Storico],
					[Id_Trader_Prec_Storico],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[CD_ATECO],
					[CD_PUNTO_FORNITURA],
					[CD_COMUNE],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[Id_Stato],
					[Note],
					[s_area_uscita]
				FROM
					[dbo].[Ssh_Utenze_Gas_Storico]
				WHERE
					[Id_UtenzeGas_Storico] = @IdUtenzeGasStorico
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_Utenze_Gas_Storico_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_Utenze_Gas_Storico table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdUtenzeGasStorico" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdTraderPrecStorico" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdCliente" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@DsCliente" type="nvarchar" direction="Input" size="60" precision="0" scale="0" param="(60)" nulldefault="null"/>
				<parameter name="@CdAteco" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@CdPuntoFornitura" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@ConsumoAnnuoStimato" type="nvarchar" direction="Input" size="38" precision="0" scale="0" param="(38)" nulldefault="null"/>
				<parameter name="@CdPrelievoAeeg" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@IdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Note" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
				<parameter name="@SAreaUscita" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id_UtenzeGas_Storico]
	, [Id_Trader_Prec_Storico]
	, [CD_CLIENTE]
	, [DS_CLIENTE]
	, [CD_ATECO]
	, [CD_PUNTO_FORNITURA]
	, [CD_COMUNE]
	, [CONSUMO_ANNUO_STIMATO]
	, [CD_PRELIEVO_AEEG]
	, [Id_Stato]
	, [Note]
	, [s_area_uscita]
    FROM
	[dbo].[Ssh_Utenze_Gas_Storico]
    WHERE 
	 ([Id_UtenzeGas_Storico] = @IdUtenzeGasStorico OR @IdUtenzeGasStorico IS NULL)
	AND ([Id_Trader_Prec_Storico] = @IdTraderPrecStorico OR @IdTraderPrecStorico IS NULL)
	AND ([CD_CLIENTE] = @CdCliente OR @CdCliente IS NULL)
	AND ([DS_CLIENTE] = @DsCliente OR @DsCliente IS NULL)
	AND ([CD_ATECO] = @CdAteco OR @CdAteco IS NULL)
	AND ([CD_PUNTO_FORNITURA] = @CdPuntoFornitura OR @CdPuntoFornitura IS NULL)
	AND ([CD_COMUNE] = @CdComune OR @CdComune IS NULL)
	AND ([CONSUMO_ANNUO_STIMATO] = @ConsumoAnnuoStimato OR @ConsumoAnnuoStimato IS NULL)
	AND ([CD_PRELIEVO_AEEG] = @CdPrelievoAeeg OR @CdPrelievoAeeg IS NULL)
	AND ([Id_Stato] = @IdStato OR @IdStato IS NULL)
	AND ([Note] = @Note OR @Note IS NULL)
	AND ([s_area_uscita] = @SAreaUscita OR @SAreaUscita IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id_UtenzeGas_Storico]
	, [Id_Trader_Prec_Storico]
	, [CD_CLIENTE]
	, [DS_CLIENTE]
	, [CD_ATECO]
	, [CD_PUNTO_FORNITURA]
	, [CD_COMUNE]
	, [CONSUMO_ANNUO_STIMATO]
	, [CD_PRELIEVO_AEEG]
	, [Id_Stato]
	, [Note]
	, [s_area_uscita]
    FROM
	[dbo].[Ssh_Utenze_Gas_Storico]
    WHERE 
	 ([Id_UtenzeGas_Storico] = @IdUtenzeGasStorico AND @IdUtenzeGasStorico is not null)
	OR ([Id_Trader_Prec_Storico] = @IdTraderPrecStorico AND @IdTraderPrecStorico is not null)
	OR ([CD_CLIENTE] = @CdCliente AND @CdCliente is not null)
	OR ([DS_CLIENTE] = @DsCliente AND @DsCliente is not null)
	OR ([CD_ATECO] = @CdAteco AND @CdAteco is not null)
	OR ([CD_PUNTO_FORNITURA] = @CdPuntoFornitura AND @CdPuntoFornitura is not null)
	OR ([CD_COMUNE] = @CdComune AND @CdComune is not null)
	OR ([CONSUMO_ANNUO_STIMATO] = @ConsumoAnnuoStimato AND @ConsumoAnnuoStimato is not null)
	OR ([CD_PRELIEVO_AEEG] = @CdPrelievoAeeg AND @CdPrelievoAeeg is not null)
	OR ([Id_Stato] = @IdStato AND @IdStato is not null)
	OR ([Note] = @Note AND @Note is not null)
	OR ([s_area_uscita] = @SAreaUscita AND @SAreaUscita is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_Utenze_Gas_Storico_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_Utenze_Gas_Storico table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id_UtenzeGas_Storico] int 
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [Id_UtenzeGas_Storico] )
				SELECT TOP {3} [Id_UtenzeGas_Storico]
				FROM [dbo].[Ssh_Utenze_Gas_Storico] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[Id_UtenzeGas_Storico], O.[Id_Trader_Prec_Storico], O.[CD_CLIENTE], O.[DS_CLIENTE], O.[CD_ATECO], O.[CD_PUNTO_FORNITURA], O.[CD_COMUNE], O.[CONSUMO_ANNUO_STIMATO], O.[CD_PRELIEVO_AEEG], O.[Id_Stato], O.[Note], O.[s_area_uscita]
				FROM
				    [dbo].[Ssh_Utenze_Gas_Storico] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[Id_UtenzeGas_Storico] = PageIndex.[Id_UtenzeGas_Storico]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_Utenze_Gas_Storico] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_SHIPPER_ATTIVI_SU_POOL_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_SHIPPER_ATTIVI_SU_POOL table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_STORICO_MENSILE_RETE],
					[CD_POOL],
					[CD_SHIPPER]
				FROM
					[dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_SHIPPER_ATTIVI_SU_POOL_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_SHIPPER_ATTIVI_SU_POOL table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_STORICO_MENSILE_RETE] nvarchar(6) COLLATE database_default , [CD_POOL] nvarchar(20) COLLATE database_default , [CD_SHIPPER] nvarchar(10) COLLATE database_default  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([CD_STORICO_MENSILE_RETE], [CD_POOL], [CD_SHIPPER])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [CD_STORICO_MENSILE_RETE], [CD_POOL], [CD_SHIPPER]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[CD_STORICO_MENSILE_RETE], O.[CD_POOL], O.[CD_SHIPPER]
				FROM
				    [dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[CD_STORICO_MENSILE_RETE] = PageIndex.[CD_STORICO_MENSILE_RETE]
					AND O.[CD_POOL] = PageIndex.[CD_POOL]
					AND O.[CD_SHIPPER] = PageIndex.[CD_SHIPPER]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_SHIPPER_ATTIVI_SU_POOL_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_SHIPPER_ATTIVI_SU_POOL table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdShipper" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL]
					(
					[CD_STORICO_MENSILE_RETE]
					,[CD_POOL]
					,[CD_SHIPPER]
					)
				VALUES
					(
					@CdStoricoMensileRete
					,@CdPool
					,@CdShipper
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_SHIPPER_ATTIVI_SU_POOL_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_SHIPPER_ATTIVI_SU_POOL table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@OriginalCdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@OriginalCdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdShipper" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@OriginalCdShipper" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL]
				SET
					[CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete
					,[CD_POOL] = @CdPool
					,[CD_SHIPPER] = @CdShipper
				WHERE
[CD_STORICO_MENSILE_RETE] = @OriginalCdStoricoMensileRete 
AND [CD_POOL] = @OriginalCdPool 
AND [CD_SHIPPER] = @OriginalCdShipper 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_SHIPPER_ATTIVI_SU_POOL_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_SHIPPER_ATTIVI_SU_POOL table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdShipper" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL] WITH (ROWLOCK) 
				WHERE
					[CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete
					AND [CD_POOL] = @CdPool
					AND [CD_SHIPPER] = @CdShipper
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_SHIPPER_ATTIVI_SU_POOL_GetByCdStoricoMensileReteCdPoolCdShipper" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_SHIPPER_ATTIVI_SU_POOL table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdShipper" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[CD_STORICO_MENSILE_RETE],
					[CD_POOL],
					[CD_SHIPPER]
				FROM
					[dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL]
				WHERE
					[CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete
					AND [CD_POOL] = @CdPool
					AND [CD_SHIPPER] = @CdShipper
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_SHIPPER_ATTIVI_SU_POOL_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_SHIPPER_ATTIVI_SU_POOL table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@CdShipper" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [CD_STORICO_MENSILE_RETE]
	, [CD_POOL]
	, [CD_SHIPPER]
    FROM
	[dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL]
    WHERE 
	 ([CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete OR @CdStoricoMensileRete IS NULL)
	AND ([CD_POOL] = @CdPool OR @CdPool IS NULL)
	AND ([CD_SHIPPER] = @CdShipper OR @CdShipper IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [CD_STORICO_MENSILE_RETE]
	, [CD_POOL]
	, [CD_SHIPPER]
    FROM
	[dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL]
    WHERE 
	 ([CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete AND @CdStoricoMensileRete is not null)
	OR ([CD_POOL] = @CdPool AND @CdPool is not null)
	OR ([CD_SHIPPER] = @CdShipper AND @CdShipper is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_SHIPPER_ATTIVI_SU_POOL_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_SHIPPER_ATTIVI_SU_POOL table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_STORICO_MENSILE_RETE] nvarchar(6) COLLATE database_default , [CD_POOL] nvarchar(20) COLLATE database_default , [CD_SHIPPER] nvarchar(10) COLLATE database_default  
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [CD_STORICO_MENSILE_RETE], [CD_POOL], [CD_SHIPPER] )
				SELECT TOP {3} [CD_STORICO_MENSILE_RETE], [CD_POOL], [CD_SHIPPER]
				FROM [dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[CD_STORICO_MENSILE_RETE], O.[CD_POOL], O.[CD_SHIPPER]
				FROM
				    [dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[CD_STORICO_MENSILE_RETE] = PageIndex.[CD_STORICO_MENSILE_RETE]
				    AND O.[CD_POOL] = PageIndex.[CD_POOL]
				    AND O.[CD_SHIPPER] = PageIndex.[CD_SHIPPER]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_SHIPPER_ATTIVI_SU_POOL] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_Stato_Relazioni_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_Stato_Relazioni table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Id_Stato],
					[Dsc_Stato]
				FROM
					[dbo].[Ssh_Stato_Relazioni]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_Stato_Relazioni_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_Stato_Relazioni table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id_Stato] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id_Stato])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id_Stato]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_Stato_Relazioni]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id_Stato], O.[Dsc_Stato]
				FROM
				    [dbo].[Ssh_Stato_Relazioni] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id_Stato] = PageIndex.[Id_Stato]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_Stato_Relazioni]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_Stato_Relazioni_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_Stato_Relazioni table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@DscStato" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_Stato_Relazioni]
					(
					[Id_Stato]
					,[Dsc_Stato]
					)
				VALUES
					(
					@IdStato
					,@DscStato
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Stato_Relazioni_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_Stato_Relazioni table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@OriginalIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@DscStato" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_Stato_Relazioni]
				SET
					[Id_Stato] = @IdStato
					,[Dsc_Stato] = @DscStato
				WHERE
[Id_Stato] = @OriginalIdStato 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Stato_Relazioni_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_Stato_Relazioni table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_Stato_Relazioni] WITH (ROWLOCK) 
				WHERE
					[Id_Stato] = @IdStato
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Stato_Relazioni_GetByIdStato" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Stato_Relazioni table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[Id_Stato],
					[Dsc_Stato]
				FROM
					[dbo].[Ssh_Stato_Relazioni]
				WHERE
					[Id_Stato] = @IdStato
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_Stato_Relazioni_GetByDscStato" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Stato_Relazioni table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@DscStato" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[Id_Stato],
					[Dsc_Stato]
				FROM
					[dbo].[Ssh_Stato_Relazioni]
				WHERE
					[Dsc_Stato] = @DscStato
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_Stato_Relazioni_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_Stato_Relazioni table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@DscStato" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id_Stato]
	, [Dsc_Stato]
    FROM
	[dbo].[Ssh_Stato_Relazioni]
    WHERE 
	 ([Id_Stato] = @IdStato OR @IdStato IS NULL)
	AND ([Dsc_Stato] = @DscStato OR @DscStato IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id_Stato]
	, [Dsc_Stato]
    FROM
	[dbo].[Ssh_Stato_Relazioni]
    WHERE 
	 ([Id_Stato] = @IdStato AND @IdStato is not null)
	OR ([Dsc_Stato] = @DscStato AND @DscStato is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_Stato_Relazioni_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_Stato_Relazioni table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id_Stato] int 
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [Id_Stato] )
				SELECT TOP {3} [Id_Stato]
				FROM [dbo].[Ssh_Stato_Relazioni] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[Id_Stato], O.[Dsc_Stato]
				FROM
				    [dbo].[Ssh_Stato_Relazioni] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[Id_Stato] = PageIndex.[Id_Stato]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_Stato_Relazioni] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_Societa_Storico_Chain_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_Societa_Storico_Chain table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[id_societa],
					[cd_societa],
					[PIva],
					[Ragione_Sociale],
					[Order_Key],
					[Id_Batch_Relazioni],
					[Id_Tipologia_Societa],
					[dt_ins],
					[IdRelazione]
				FROM
					[dbo].[Ssh_Societa_Storico_Chain]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_Societa_Storico_Chain_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_Societa_Storico_Chain table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [id_societa] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([id_societa])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [id_societa]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_Societa_Storico_Chain]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[id_societa], O.[cd_societa], O.[PIva], O.[Ragione_Sociale], O.[Order_Key], O.[Id_Batch_Relazioni], O.[Id_Tipologia_Societa], O.[dt_ins], O.[IdRelazione]
				FROM
				    [dbo].[Ssh_Societa_Storico_Chain] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[id_societa] = PageIndex.[id_societa]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_Societa_Storico_Chain]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_Societa_Storico_Chain_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_Societa_Storico_Chain table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdSocieta" type="int" direction="Output" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@CdSocieta" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@Piva" type="nvarchar" direction="Input" size="30" precision="0" scale="0" param="(30)" nulldefault=""/>
				<parameter name="@RagioneSociale" type="nvarchar" direction="Input" size="100" precision="0" scale="0" param="(100)" nulldefault=""/>
				<parameter name="@OrderKey" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@IdBatchRelazioni" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@DtIns" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault=""/>
				<parameter name="@IdRelazione" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_Societa_Storico_Chain]
					(
					[cd_societa]
					,[PIva]
					,[Ragione_Sociale]
					,[Order_Key]
					,[Id_Batch_Relazioni]
					,[Id_Tipologia_Societa]
					,[dt_ins]
					,[IdRelazione]
					)
				VALUES
					(
					@CdSocieta
					,@Piva
					,@RagioneSociale
					,@OrderKey
					,@IdBatchRelazioni
					,@IdTipologiaSocieta
					,@DtIns
					,@IdRelazione
					)
				
				-- Get the identity value
				SET @IdSocieta = SCOPE_IDENTITY()
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Societa_Storico_Chain_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_Societa_Storico_Chain table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@CdSocieta" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@Piva" type="nvarchar" direction="Input" size="30" precision="0" scale="0" param="(30)" nulldefault=""/>
				<parameter name="@RagioneSociale" type="nvarchar" direction="Input" size="100" precision="0" scale="0" param="(100)" nulldefault=""/>
				<parameter name="@OrderKey" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@IdBatchRelazioni" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@DtIns" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault=""/>
				<parameter name="@IdRelazione" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_Societa_Storico_Chain]
				SET
					[cd_societa] = @CdSocieta
					,[PIva] = @Piva
					,[Ragione_Sociale] = @RagioneSociale
					,[Order_Key] = @OrderKey
					,[Id_Batch_Relazioni] = @IdBatchRelazioni
					,[Id_Tipologia_Societa] = @IdTipologiaSocieta
					,[dt_ins] = @DtIns
					,[IdRelazione] = @IdRelazione
				WHERE
[id_societa] = @IdSocieta 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Societa_Storico_Chain_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_Societa_Storico_Chain table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_Societa_Storico_Chain] WITH (ROWLOCK) 
				WHERE
					[id_societa] = @IdSocieta
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Societa_Storico_Chain_GetByIdTipologiaSocieta" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Societa_Storico_Chain table through a foreign key
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SET ANSI_NULLS OFF
				
				SELECT
					[id_societa],
					[cd_societa],
					[PIva],
					[Ragione_Sociale],
					[Order_Key],
					[Id_Batch_Relazioni],
					[Id_Tipologia_Societa],
					[dt_ins],
					[IdRelazione]
				FROM
					[dbo].[Ssh_Societa_Storico_Chain]
				WHERE
					[Id_Tipologia_Societa] = @IdTipologiaSocieta
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Societa_Storico_Chain_GetByIdBatchRelazioni" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Societa_Storico_Chain table through a foreign key
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdBatchRelazioni" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SET ANSI_NULLS OFF
				
				SELECT
					[id_societa],
					[cd_societa],
					[PIva],
					[Ragione_Sociale],
					[Order_Key],
					[Id_Batch_Relazioni],
					[Id_Tipologia_Societa],
					[dt_ins],
					[IdRelazione]
				FROM
					[dbo].[Ssh_Societa_Storico_Chain]
				WHERE
					[Id_Batch_Relazioni] = @IdBatchRelazioni
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_Societa_Storico_Chain_GetByIdSocieta" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_Societa_Storico_Chain table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[id_societa],
					[cd_societa],
					[PIva],
					[Ragione_Sociale],
					[Order_Key],
					[Id_Batch_Relazioni],
					[Id_Tipologia_Societa],
					[dt_ins],
					[IdRelazione]
				FROM
					[dbo].[Ssh_Societa_Storico_Chain]
				WHERE
					[id_societa] = @IdSocieta
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_Societa_Storico_Chain_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_Societa_Storico_Chain table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdSocieta" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
				<parameter name="@Piva" type="nvarchar" direction="Input" size="30" precision="0" scale="0" param="(30)" nulldefault="null"/>
				<parameter name="@RagioneSociale" type="nvarchar" direction="Input" size="100" precision="0" scale="0" param="(100)" nulldefault="null"/>
				<parameter name="@OrderKey" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdBatchRelazioni" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@DtIns" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault="null"/>
				<parameter name="@IdRelazione" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [id_societa]
	, [cd_societa]
	, [PIva]
	, [Ragione_Sociale]
	, [Order_Key]
	, [Id_Batch_Relazioni]
	, [Id_Tipologia_Societa]
	, [dt_ins]
	, [IdRelazione]
    FROM
	[dbo].[Ssh_Societa_Storico_Chain]
    WHERE 
	 ([id_societa] = @IdSocieta OR @IdSocieta IS NULL)
	AND ([cd_societa] = @CdSocieta OR @CdSocieta IS NULL)
	AND ([PIva] = @Piva OR @Piva IS NULL)
	AND ([Ragione_Sociale] = @RagioneSociale OR @RagioneSociale IS NULL)
	AND ([Order_Key] = @OrderKey OR @OrderKey IS NULL)
	AND ([Id_Batch_Relazioni] = @IdBatchRelazioni OR @IdBatchRelazioni IS NULL)
	AND ([Id_Tipologia_Societa] = @IdTipologiaSocieta OR @IdTipologiaSocieta IS NULL)
	AND ([dt_ins] = @DtIns OR @DtIns IS NULL)
	AND ([IdRelazione] = @IdRelazione OR @IdRelazione IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [id_societa]
	, [cd_societa]
	, [PIva]
	, [Ragione_Sociale]
	, [Order_Key]
	, [Id_Batch_Relazioni]
	, [Id_Tipologia_Societa]
	, [dt_ins]
	, [IdRelazione]
    FROM
	[dbo].[Ssh_Societa_Storico_Chain]
    WHERE 
	 ([id_societa] = @IdSocieta AND @IdSocieta is not null)
	OR ([cd_societa] = @CdSocieta AND @CdSocieta is not null)
	OR ([PIva] = @Piva AND @Piva is not null)
	OR ([Ragione_Sociale] = @RagioneSociale AND @RagioneSociale is not null)
	OR ([Order_Key] = @OrderKey AND @OrderKey is not null)
	OR ([Id_Batch_Relazioni] = @IdBatchRelazioni AND @IdBatchRelazioni is not null)
	OR ([Id_Tipologia_Societa] = @IdTipologiaSocieta AND @IdTipologiaSocieta is not null)
	OR ([dt_ins] = @DtIns AND @DtIns is not null)
	OR ([IdRelazione] = @IdRelazione AND @IdRelazione is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_Societa_Storico_Chain_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_Societa_Storico_Chain table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [id_societa] int 
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [id_societa] )
				SELECT TOP {3} [id_societa]
				FROM [dbo].[Ssh_Societa_Storico_Chain] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[id_societa], O.[cd_societa], O.[PIva], O.[Ragione_Sociale], O.[Order_Key], O.[Id_Batch_Relazioni], O.[Id_Tipologia_Societa], O.[dt_ins], O.[IdRelazione]
				FROM
				    [dbo].[Ssh_Societa_Storico_Chain] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[id_societa] = PageIndex.[id_societa]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_Societa_Storico_Chain] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_UTENZE_GAS_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_UTENZE_GAS table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_MESE_INIZIO_FORNITURA],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[CD_ATECO],
					[CD_PUNTO_FORNITURA],
					[CD_POOL],
					[CD_COMUNE],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[CD_TRADER_PRECEDENTE],
					[CD_STATO_PUNTO],
					[S_AREA_USCITA]
				FROM
					[dbo].[Ssh_UTENZE_GAS]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_UTENZE_GAS_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_UTENZE_GAS table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_MESE_INIZIO_FORNITURA] nvarchar(6) COLLATE database_default , [CD_PUNTO_FORNITURA] nvarchar(10) COLLATE database_default  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([CD_MESE_INIZIO_FORNITURA], [CD_PUNTO_FORNITURA])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [CD_MESE_INIZIO_FORNITURA], [CD_PUNTO_FORNITURA]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_UTENZE_GAS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[CD_MESE_INIZIO_FORNITURA], O.[CD_CLIENTE], O.[DS_CLIENTE], O.[CD_ATECO], O.[CD_PUNTO_FORNITURA], O.[CD_POOL], O.[CD_COMUNE], O.[CONSUMO_ANNUO_STIMATO], O.[CD_PRELIEVO_AEEG], O.[CD_TRADER_PRECEDENTE], O.[CD_STATO_PUNTO], O.[S_AREA_USCITA]
				FROM
				    [dbo].[Ssh_UTENZE_GAS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[CD_MESE_INIZIO_FORNITURA] = PageIndex.[CD_MESE_INIZIO_FORNITURA]
					AND O.[CD_PUNTO_FORNITURA] = PageIndex.[CD_PUNTO_FORNITURA]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_UTENZE_GAS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_UTENZE_GAS_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_UTENZE_GAS table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdMeseInizioFornitura" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdCliente" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@DsCliente" type="nvarchar" direction="Input" size="60" precision="0" scale="0" param="(60)" nulldefault=""/>
				<parameter name="@CdAteco" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdPuntoFornitura" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@ConsumoAnnuoStimato" type="nvarchar" direction="Input" size="38" precision="0" scale="0" param="(38)" nulldefault=""/>
				<parameter name="@CdPrelievoAeeg" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdTraderPrecedente" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdStatoPunto" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@SAreaUscita" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_UTENZE_GAS]
					(
					[CD_MESE_INIZIO_FORNITURA]
					,[CD_CLIENTE]
					,[DS_CLIENTE]
					,[CD_ATECO]
					,[CD_PUNTO_FORNITURA]
					,[CD_POOL]
					,[CD_COMUNE]
					,[CONSUMO_ANNUO_STIMATO]
					,[CD_PRELIEVO_AEEG]
					,[CD_TRADER_PRECEDENTE]
					,[CD_STATO_PUNTO]
					,[S_AREA_USCITA]
					)
				VALUES
					(
					@CdMeseInizioFornitura
					,@CdCliente
					,@DsCliente
					,@CdAteco
					,@CdPuntoFornitura
					,@CdPool
					,@CdComune
					,@ConsumoAnnuoStimato
					,@CdPrelievoAeeg
					,@CdTraderPrecedente
					,@CdStatoPunto
					,@SAreaUscita
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_UTENZE_GAS_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_UTENZE_GAS table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdMeseInizioFornitura" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@OriginalCdMeseInizioFornitura" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdCliente" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@DsCliente" type="nvarchar" direction="Input" size="60" precision="0" scale="0" param="(60)" nulldefault=""/>
				<parameter name="@CdAteco" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdPuntoFornitura" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@OriginalCdPuntoFornitura" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@ConsumoAnnuoStimato" type="nvarchar" direction="Input" size="38" precision="0" scale="0" param="(38)" nulldefault=""/>
				<parameter name="@CdPrelievoAeeg" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdTraderPrecedente" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdStatoPunto" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
				<parameter name="@SAreaUscita" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_UTENZE_GAS]
				SET
					[CD_MESE_INIZIO_FORNITURA] = @CdMeseInizioFornitura
					,[CD_CLIENTE] = @CdCliente
					,[DS_CLIENTE] = @DsCliente
					,[CD_ATECO] = @CdAteco
					,[CD_PUNTO_FORNITURA] = @CdPuntoFornitura
					,[CD_POOL] = @CdPool
					,[CD_COMUNE] = @CdComune
					,[CONSUMO_ANNUO_STIMATO] = @ConsumoAnnuoStimato
					,[CD_PRELIEVO_AEEG] = @CdPrelievoAeeg
					,[CD_TRADER_PRECEDENTE] = @CdTraderPrecedente
					,[CD_STATO_PUNTO] = @CdStatoPunto
					,[S_AREA_USCITA] = @SAreaUscita
				WHERE
[CD_MESE_INIZIO_FORNITURA] = @OriginalCdMeseInizioFornitura 
AND [CD_PUNTO_FORNITURA] = @OriginalCdPuntoFornitura 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_UTENZE_GAS_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_UTENZE_GAS table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdMeseInizioFornitura" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPuntoFornitura" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_UTENZE_GAS] WITH (ROWLOCK) 
				WHERE
					[CD_MESE_INIZIO_FORNITURA] = @CdMeseInizioFornitura
					AND [CD_PUNTO_FORNITURA] = @CdPuntoFornitura
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_UTENZE_GAS_GetByCdMeseInizioFornituraCdPuntoFornitura" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_UTENZE_GAS table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdMeseInizioFornitura" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPuntoFornitura" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[CD_MESE_INIZIO_FORNITURA],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[CD_ATECO],
					[CD_PUNTO_FORNITURA],
					[CD_POOL],
					[CD_COMUNE],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[CD_TRADER_PRECEDENTE],
					[CD_STATO_PUNTO],
					[S_AREA_USCITA]
				FROM
					[dbo].[Ssh_UTENZE_GAS]
				WHERE
					[CD_MESE_INIZIO_FORNITURA] = @CdMeseInizioFornitura
					AND [CD_PUNTO_FORNITURA] = @CdPuntoFornitura
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_UTENZE_GAS_GetByCdComune" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_UTENZE_GAS table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[CD_MESE_INIZIO_FORNITURA],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[CD_ATECO],
					[CD_PUNTO_FORNITURA],
					[CD_POOL],
					[CD_COMUNE],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[CD_TRADER_PRECEDENTE],
					[CD_STATO_PUNTO],
					[S_AREA_USCITA]
				FROM
					[dbo].[Ssh_UTENZE_GAS]
				WHERE
					[CD_COMUNE] = @CdComune
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_UTENZE_GAS_GetByCdPrelievoAeeg" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_UTENZE_GAS table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdPrelievoAeeg" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[CD_MESE_INIZIO_FORNITURA],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[CD_ATECO],
					[CD_PUNTO_FORNITURA],
					[CD_POOL],
					[CD_COMUNE],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[CD_TRADER_PRECEDENTE],
					[CD_STATO_PUNTO],
					[S_AREA_USCITA]
				FROM
					[dbo].[Ssh_UTENZE_GAS]
				WHERE
					[CD_PRELIEVO_AEEG] = @CdPrelievoAeeg
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_UTENZE_GAS_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_UTENZE_GAS table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdMeseInizioFornitura" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@CdCliente" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@DsCliente" type="nvarchar" direction="Input" size="60" precision="0" scale="0" param="(60)" nulldefault="null"/>
				<parameter name="@CdAteco" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@CdPuntoFornitura" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@ConsumoAnnuoStimato" type="nvarchar" direction="Input" size="38" precision="0" scale="0" param="(38)" nulldefault="null"/>
				<parameter name="@CdPrelievoAeeg" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@CdTraderPrecedente" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@CdStatoPunto" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
				<parameter name="@SAreaUscita" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [CD_MESE_INIZIO_FORNITURA]
	, [CD_CLIENTE]
	, [DS_CLIENTE]
	, [CD_ATECO]
	, [CD_PUNTO_FORNITURA]
	, [CD_POOL]
	, [CD_COMUNE]
	, [CONSUMO_ANNUO_STIMATO]
	, [CD_PRELIEVO_AEEG]
	, [CD_TRADER_PRECEDENTE]
	, [CD_STATO_PUNTO]
	, [S_AREA_USCITA]
    FROM
	[dbo].[Ssh_UTENZE_GAS]
    WHERE 
	 ([CD_MESE_INIZIO_FORNITURA] = @CdMeseInizioFornitura OR @CdMeseInizioFornitura IS NULL)
	AND ([CD_CLIENTE] = @CdCliente OR @CdCliente IS NULL)
	AND ([DS_CLIENTE] = @DsCliente OR @DsCliente IS NULL)
	AND ([CD_ATECO] = @CdAteco OR @CdAteco IS NULL)
	AND ([CD_PUNTO_FORNITURA] = @CdPuntoFornitura OR @CdPuntoFornitura IS NULL)
	AND ([CD_POOL] = @CdPool OR @CdPool IS NULL)
	AND ([CD_COMUNE] = @CdComune OR @CdComune IS NULL)
	AND ([CONSUMO_ANNUO_STIMATO] = @ConsumoAnnuoStimato OR @ConsumoAnnuoStimato IS NULL)
	AND ([CD_PRELIEVO_AEEG] = @CdPrelievoAeeg OR @CdPrelievoAeeg IS NULL)
	AND ([CD_TRADER_PRECEDENTE] = @CdTraderPrecedente OR @CdTraderPrecedente IS NULL)
	AND ([CD_STATO_PUNTO] = @CdStatoPunto OR @CdStatoPunto IS NULL)
	AND ([S_AREA_USCITA] = @SAreaUscita OR @SAreaUscita IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [CD_MESE_INIZIO_FORNITURA]
	, [CD_CLIENTE]
	, [DS_CLIENTE]
	, [CD_ATECO]
	, [CD_PUNTO_FORNITURA]
	, [CD_POOL]
	, [CD_COMUNE]
	, [CONSUMO_ANNUO_STIMATO]
	, [CD_PRELIEVO_AEEG]
	, [CD_TRADER_PRECEDENTE]
	, [CD_STATO_PUNTO]
	, [S_AREA_USCITA]
    FROM
	[dbo].[Ssh_UTENZE_GAS]
    WHERE 
	 ([CD_MESE_INIZIO_FORNITURA] = @CdMeseInizioFornitura AND @CdMeseInizioFornitura is not null)
	OR ([CD_CLIENTE] = @CdCliente AND @CdCliente is not null)
	OR ([DS_CLIENTE] = @DsCliente AND @DsCliente is not null)
	OR ([CD_ATECO] = @CdAteco AND @CdAteco is not null)
	OR ([CD_PUNTO_FORNITURA] = @CdPuntoFornitura AND @CdPuntoFornitura is not null)
	OR ([CD_POOL] = @CdPool AND @CdPool is not null)
	OR ([CD_COMUNE] = @CdComune AND @CdComune is not null)
	OR ([CONSUMO_ANNUO_STIMATO] = @ConsumoAnnuoStimato AND @ConsumoAnnuoStimato is not null)
	OR ([CD_PRELIEVO_AEEG] = @CdPrelievoAeeg AND @CdPrelievoAeeg is not null)
	OR ([CD_TRADER_PRECEDENTE] = @CdTraderPrecedente AND @CdTraderPrecedente is not null)
	OR ([CD_STATO_PUNTO] = @CdStatoPunto AND @CdStatoPunto is not null)
	OR ([S_AREA_USCITA] = @SAreaUscita AND @SAreaUscita is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_UTENZE_GAS_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_UTENZE_GAS table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_MESE_INIZIO_FORNITURA] nvarchar(6) COLLATE database_default , [CD_PUNTO_FORNITURA] nvarchar(10) COLLATE database_default  
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [CD_MESE_INIZIO_FORNITURA], [CD_PUNTO_FORNITURA] )
				SELECT TOP {3} [CD_MESE_INIZIO_FORNITURA], [CD_PUNTO_FORNITURA]
				FROM [dbo].[Ssh_UTENZE_GAS] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[CD_MESE_INIZIO_FORNITURA], O.[CD_CLIENTE], O.[DS_CLIENTE], O.[CD_ATECO], O.[CD_PUNTO_FORNITURA], O.[CD_POOL], O.[CD_COMUNE], O.[CONSUMO_ANNUO_STIMATO], O.[CD_PRELIEVO_AEEG], O.[CD_TRADER_PRECEDENTE], O.[CD_STATO_PUNTO], O.[S_AREA_USCITA]
				FROM
				    [dbo].[Ssh_UTENZE_GAS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[CD_MESE_INIZIO_FORNITURA] = PageIndex.[CD_MESE_INIZIO_FORNITURA]
				    AND O.[CD_PUNTO_FORNITURA] = PageIndex.[CD_PUNTO_FORNITURA]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_UTENZE_GAS] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_NewSocieta_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_NewSocieta table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Piva],
					[Ds_Societa],
					[Id_Tipologia_Societa],
					[dt_ins],
					[Cd_Storico_Mensile_Rete],
					[Cd_Societa]
				FROM
					[dbo].[Ssh_NewSocieta]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_NewSocieta_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_NewSocieta table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Cd_Societa] nchar(30) COLLATE database_default  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Cd_Societa])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Cd_Societa]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_NewSocieta]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Piva], O.[Ds_Societa], O.[Id_Tipologia_Societa], O.[dt_ins], O.[Cd_Storico_Mensile_Rete], O.[Cd_Societa]
				FROM
				    [dbo].[Ssh_NewSocieta] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Cd_Societa] = PageIndex.[Cd_Societa]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_NewSocieta]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_NewSocieta_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_NewSocieta table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@Piva" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
				<parameter name="@DsSocieta" type="nvarchar" direction="Input" size="100" precision="0" scale="0" param="(100)" nulldefault=""/>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@DtIns" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault=""/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdSocieta" type="nchar" direction="Input" size="30" precision="0" scale="0" param="(30)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_NewSocieta]
					(
					[Piva]
					,[Ds_Societa]
					,[Id_Tipologia_Societa]
					,[dt_ins]
					,[Cd_Storico_Mensile_Rete]
					,[Cd_Societa]
					)
				VALUES
					(
					@Piva
					,@DsSocieta
					,@IdTipologiaSocieta
					,@DtIns
					,@CdStoricoMensileRete
					,@CdSocieta
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_NewSocieta_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_NewSocieta table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@Piva" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
				<parameter name="@DsSocieta" type="nvarchar" direction="Input" size="100" precision="0" scale="0" param="(100)" nulldefault=""/>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
				<parameter name="@DtIns" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault=""/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdSocieta" type="nchar" direction="Input" size="30" precision="0" scale="0" param="(30)" nulldefault=""/>
				<parameter name="@OriginalCdSocieta" type="nchar" direction="Input" size="30" precision="0" scale="0" param="(30)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_NewSocieta]
				SET
					[Piva] = @Piva
					,[Ds_Societa] = @DsSocieta
					,[Id_Tipologia_Societa] = @IdTipologiaSocieta
					,[dt_ins] = @DtIns
					,[Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete
					,[Cd_Societa] = @CdSocieta
				WHERE
[Cd_Societa] = @OriginalCdSocieta 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_NewSocieta_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_NewSocieta table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdSocieta" type="nchar" direction="Input" size="30" precision="0" scale="0" param="(30)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_NewSocieta] WITH (ROWLOCK) 
				WHERE
					[Cd_Societa] = @CdSocieta
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_NewSocieta_GetByIdTipologiaSocieta" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_NewSocieta table through a foreign key
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SET ANSI_NULLS OFF
				
				SELECT
					[Piva],
					[Ds_Societa],
					[Id_Tipologia_Societa],
					[dt_ins],
					[Cd_Storico_Mensile_Rete],
					[Cd_Societa]
				FROM
					[dbo].[Ssh_NewSocieta]
				WHERE
					[Id_Tipologia_Societa] = @IdTipologiaSocieta
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_NewSocieta_GetByCdSocieta" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_NewSocieta table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdSocieta" type="nchar" direction="Input" size="30" precision="0" scale="0" param="(30)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[Piva],
					[Ds_Societa],
					[Id_Tipologia_Societa],
					[dt_ins],
					[Cd_Storico_Mensile_Rete],
					[Cd_Societa]
				FROM
					[dbo].[Ssh_NewSocieta]
				WHERE
					[Cd_Societa] = @CdSocieta
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_NewSocieta_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_NewSocieta table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@Piva" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault="null"/>
				<parameter name="@DsSocieta" type="nvarchar" direction="Input" size="100" precision="0" scale="0" param="(100)" nulldefault="null"/>
				<parameter name="@IdTipologiaSocieta" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@DtIns" type="datetime" direction="Input" size="8" precision="23" scale="3" param="" nulldefault="null"/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@CdSocieta" type="nchar" direction="Input" size="30" precision="0" scale="0" param="(30)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Piva]
	, [Ds_Societa]
	, [Id_Tipologia_Societa]
	, [dt_ins]
	, [Cd_Storico_Mensile_Rete]
	, [Cd_Societa]
    FROM
	[dbo].[Ssh_NewSocieta]
    WHERE 
	 ([Piva] = @Piva OR @Piva IS NULL)
	AND ([Ds_Societa] = @DsSocieta OR @DsSocieta IS NULL)
	AND ([Id_Tipologia_Societa] = @IdTipologiaSocieta OR @IdTipologiaSocieta IS NULL)
	AND ([dt_ins] = @DtIns OR @DtIns IS NULL)
	AND ([Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete OR @CdStoricoMensileRete IS NULL)
	AND ([Cd_Societa] = @CdSocieta OR @CdSocieta IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Piva]
	, [Ds_Societa]
	, [Id_Tipologia_Societa]
	, [dt_ins]
	, [Cd_Storico_Mensile_Rete]
	, [Cd_Societa]
    FROM
	[dbo].[Ssh_NewSocieta]
    WHERE 
	 ([Piva] = @Piva AND @Piva is not null)
	OR ([Ds_Societa] = @DsSocieta AND @DsSocieta is not null)
	OR ([Id_Tipologia_Societa] = @IdTipologiaSocieta AND @IdTipologiaSocieta is not null)
	OR ([dt_ins] = @DtIns AND @DtIns is not null)
	OR ([Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete AND @CdStoricoMensileRete is not null)
	OR ([Cd_Societa] = @CdSocieta AND @CdSocieta is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_NewSocieta_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_NewSocieta table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Cd_Societa] nchar(30) COLLATE database_default  
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [Cd_Societa] )
				SELECT TOP {3} [Cd_Societa]
				FROM [dbo].[Ssh_NewSocieta] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[Piva], O.[Ds_Societa], O.[Id_Tipologia_Societa], O.[dt_ins], O.[Cd_Storico_Mensile_Rete], O.[Cd_Societa]
				FROM
				    [dbo].[Ssh_NewSocieta] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[Cd_Societa] = PageIndex.[Cd_Societa]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_NewSocieta] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_TIPOLOG_PRELIEVO_AEEG_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_TIPOLOG_PRELIEVO_AEEG table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_PRELIEVO_AEEG],
					[DS_PRELIEVO_AEEG]
				FROM
					[dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_TIPOLOG_PRELIEVO_AEEG_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_TIPOLOG_PRELIEVO_AEEG table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_PRELIEVO_AEEG] nvarchar(5) COLLATE database_default  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([CD_PRELIEVO_AEEG])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [CD_PRELIEVO_AEEG]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[CD_PRELIEVO_AEEG], O.[DS_PRELIEVO_AEEG]
				FROM
				    [dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[CD_PRELIEVO_AEEG] = PageIndex.[CD_PRELIEVO_AEEG]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_TIPOLOG_PRELIEVO_AEEG_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_TIPOLOG_PRELIEVO_AEEG table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdPrelievoAeeg" type="nvarchar" direction="Input" size="5" precision="0" scale="0" param="(5)" nulldefault=""/>
				<parameter name="@DsPrelievoAeeg" type="nvarchar" direction="Input" size="60" precision="0" scale="0" param="(60)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG]
					(
					[CD_PRELIEVO_AEEG]
					,[DS_PRELIEVO_AEEG]
					)
				VALUES
					(
					@CdPrelievoAeeg
					,@DsPrelievoAeeg
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_TIPOLOG_PRELIEVO_AEEG_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_TIPOLOG_PRELIEVO_AEEG table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdPrelievoAeeg" type="nvarchar" direction="Input" size="5" precision="0" scale="0" param="(5)" nulldefault=""/>
				<parameter name="@OriginalCdPrelievoAeeg" type="nvarchar" direction="Input" size="5" precision="0" scale="0" param="(5)" nulldefault=""/>
				<parameter name="@DsPrelievoAeeg" type="nvarchar" direction="Input" size="60" precision="0" scale="0" param="(60)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG]
				SET
					[CD_PRELIEVO_AEEG] = @CdPrelievoAeeg
					,[DS_PRELIEVO_AEEG] = @DsPrelievoAeeg
				WHERE
[CD_PRELIEVO_AEEG] = @OriginalCdPrelievoAeeg 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_TIPOLOG_PRELIEVO_AEEG_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_TIPOLOG_PRELIEVO_AEEG table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdPrelievoAeeg" type="nvarchar" direction="Input" size="5" precision="0" scale="0" param="(5)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG] WITH (ROWLOCK) 
				WHERE
					[CD_PRELIEVO_AEEG] = @CdPrelievoAeeg
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_TIPOLOG_PRELIEVO_AEEG_GetByCdPrelievoAeeg" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_TIPOLOG_PRELIEVO_AEEG table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdPrelievoAeeg" type="nvarchar" direction="Input" size="5" precision="0" scale="0" param="(5)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[CD_PRELIEVO_AEEG],
					[DS_PRELIEVO_AEEG]
				FROM
					[dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG]
				WHERE
					[CD_PRELIEVO_AEEG] = @CdPrelievoAeeg
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_TIPOLOG_PRELIEVO_AEEG_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_TIPOLOG_PRELIEVO_AEEG table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdPrelievoAeeg" type="nvarchar" direction="Input" size="5" precision="0" scale="0" param="(5)" nulldefault="null"/>
				<parameter name="@DsPrelievoAeeg" type="nvarchar" direction="Input" size="60" precision="0" scale="0" param="(60)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [CD_PRELIEVO_AEEG]
	, [DS_PRELIEVO_AEEG]
    FROM
	[dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG]
    WHERE 
	 ([CD_PRELIEVO_AEEG] = @CdPrelievoAeeg OR @CdPrelievoAeeg IS NULL)
	AND ([DS_PRELIEVO_AEEG] = @DsPrelievoAeeg OR @DsPrelievoAeeg IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [CD_PRELIEVO_AEEG]
	, [DS_PRELIEVO_AEEG]
    FROM
	[dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG]
    WHERE 
	 ([CD_PRELIEVO_AEEG] = @CdPrelievoAeeg AND @CdPrelievoAeeg is not null)
	OR ([DS_PRELIEVO_AEEG] = @DsPrelievoAeeg AND @DsPrelievoAeeg is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_TIPOLOG_PRELIEVO_AEEG_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_TIPOLOG_PRELIEVO_AEEG table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_PRELIEVO_AEEG] nvarchar(5) COLLATE database_default  
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [CD_PRELIEVO_AEEG] )
				SELECT TOP {3} [CD_PRELIEVO_AEEG]
				FROM [dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[CD_PRELIEVO_AEEG], O.[DS_PRELIEVO_AEEG]
				FROM
				    [dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[CD_PRELIEVO_AEEG] = PageIndex.[CD_PRELIEVO_AEEG]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_TIPOLOG_PRELIEVO_AEEG] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_POOL_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_POOL table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_STORICO_MENSILE_RETE],
					[CD_POOL],
					[CD_TRASPORTATORE],
					[CD_COMUNE],
					[CD_DISTRIBUTORE],
					[CD_SHIPPER_DEFAULT]
				FROM
					[dbo].[Ssh_POOL]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_POOL_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_POOL table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_STORICO_MENSILE_RETE] nvarchar(6) COLLATE database_default , [CD_POOL] nvarchar(20) COLLATE database_default , [CD_COMUNE] nvarchar(10) COLLATE database_default  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([CD_STORICO_MENSILE_RETE], [CD_POOL], [CD_COMUNE])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [CD_STORICO_MENSILE_RETE], [CD_POOL], [CD_COMUNE]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_POOL]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[CD_STORICO_MENSILE_RETE], O.[CD_POOL], O.[CD_TRASPORTATORE], O.[CD_COMUNE], O.[CD_DISTRIBUTORE], O.[CD_SHIPPER_DEFAULT]
				FROM
				    [dbo].[Ssh_POOL] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[CD_STORICO_MENSILE_RETE] = PageIndex.[CD_STORICO_MENSILE_RETE]
					AND O.[CD_POOL] = PageIndex.[CD_POOL]
					AND O.[CD_COMUNE] = PageIndex.[CD_COMUNE]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_POOL]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_POOL_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_POOL table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdTrasportatore" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdDistributore" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdShipperDefault" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_POOL]
					(
					[CD_STORICO_MENSILE_RETE]
					,[CD_POOL]
					,[CD_TRASPORTATORE]
					,[CD_COMUNE]
					,[CD_DISTRIBUTORE]
					,[CD_SHIPPER_DEFAULT]
					)
				VALUES
					(
					@CdStoricoMensileRete
					,@CdPool
					,@CdTrasportatore
					,@CdComune
					,@CdDistributore
					,@CdShipperDefault
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_POOL_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_POOL table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@OriginalCdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@OriginalCdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdTrasportatore" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@OriginalCdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdDistributore" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@CdShipperDefault" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_POOL]
				SET
					[CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete
					,[CD_POOL] = @CdPool
					,[CD_TRASPORTATORE] = @CdTrasportatore
					,[CD_COMUNE] = @CdComune
					,[CD_DISTRIBUTORE] = @CdDistributore
					,[CD_SHIPPER_DEFAULT] = @CdShipperDefault
				WHERE
[CD_STORICO_MENSILE_RETE] = @OriginalCdStoricoMensileRete 
AND [CD_POOL] = @OriginalCdPool 
AND [CD_COMUNE] = @OriginalCdComune 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_POOL_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_POOL table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_POOL] WITH (ROWLOCK) 
				WHERE
					[CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete
					AND [CD_POOL] = @CdPool
					AND [CD_COMUNE] = @CdComune
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_POOL_GetByCdStoricoMensileReteCdPoolCdComune" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_POOL table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[CD_STORICO_MENSILE_RETE],
					[CD_POOL],
					[CD_TRASPORTATORE],
					[CD_COMUNE],
					[CD_DISTRIBUTORE],
					[CD_SHIPPER_DEFAULT]
				FROM
					[dbo].[Ssh_POOL]
				WHERE
					[CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete
					AND [CD_POOL] = @CdPool
					AND [CD_COMUNE] = @CdComune
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_POOL_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_POOL table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@CdTrasportatore" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@CdComune" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@CdDistributore" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@CdShipperDefault" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [CD_STORICO_MENSILE_RETE]
	, [CD_POOL]
	, [CD_TRASPORTATORE]
	, [CD_COMUNE]
	, [CD_DISTRIBUTORE]
	, [CD_SHIPPER_DEFAULT]
    FROM
	[dbo].[Ssh_POOL]
    WHERE 
	 ([CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete OR @CdStoricoMensileRete IS NULL)
	AND ([CD_POOL] = @CdPool OR @CdPool IS NULL)
	AND ([CD_TRASPORTATORE] = @CdTrasportatore OR @CdTrasportatore IS NULL)
	AND ([CD_COMUNE] = @CdComune OR @CdComune IS NULL)
	AND ([CD_DISTRIBUTORE] = @CdDistributore OR @CdDistributore IS NULL)
	AND ([CD_SHIPPER_DEFAULT] = @CdShipperDefault OR @CdShipperDefault IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [CD_STORICO_MENSILE_RETE]
	, [CD_POOL]
	, [CD_TRASPORTATORE]
	, [CD_COMUNE]
	, [CD_DISTRIBUTORE]
	, [CD_SHIPPER_DEFAULT]
    FROM
	[dbo].[Ssh_POOL]
    WHERE 
	 ([CD_STORICO_MENSILE_RETE] = @CdStoricoMensileRete AND @CdStoricoMensileRete is not null)
	OR ([CD_POOL] = @CdPool AND @CdPool is not null)
	OR ([CD_TRASPORTATORE] = @CdTrasportatore AND @CdTrasportatore is not null)
	OR ([CD_COMUNE] = @CdComune AND @CdComune is not null)
	OR ([CD_DISTRIBUTORE] = @CdDistributore AND @CdDistributore is not null)
	OR ([CD_SHIPPER_DEFAULT] = @CdShipperDefault AND @CdShipperDefault is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_POOL_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_POOL table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_STORICO_MENSILE_RETE] nvarchar(6) COLLATE database_default , [CD_POOL] nvarchar(20) COLLATE database_default , [CD_COMUNE] nvarchar(10) COLLATE database_default  
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [CD_STORICO_MENSILE_RETE], [CD_POOL], [CD_COMUNE] )
				SELECT TOP {3} [CD_STORICO_MENSILE_RETE], [CD_POOL], [CD_COMUNE]
				FROM [dbo].[Ssh_POOL] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[CD_STORICO_MENSILE_RETE], O.[CD_POOL], O.[CD_TRASPORTATORE], O.[CD_COMUNE], O.[CD_DISTRIBUTORE], O.[CD_SHIPPER_DEFAULT]
				FROM
				    [dbo].[Ssh_POOL] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[CD_STORICO_MENSILE_RETE] = PageIndex.[CD_STORICO_MENSILE_RETE]
				    AND O.[CD_POOL] = PageIndex.[CD_POOL]
				    AND O.[CD_COMUNE] = PageIndex.[CD_COMUNE]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_POOL] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_New_Relazioni_Societarie_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_New_Relazioni_Societarie table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Cd_Storico_Mensile_Rete],
					[Cd_Pool],
					[Cd_Societa1],
					[Cd_Societa2],
					[id_Tipologia_Societa2]
				FROM
					[dbo].[Ssh_New_Relazioni_Societarie]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_New_Relazioni_Societarie_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_New_Relazioni_Societarie table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Cd_Storico_Mensile_Rete] nvarchar(6) COLLATE database_default , [Cd_Pool] nvarchar(20) COLLATE database_default , [Cd_Societa1] nvarchar(25) COLLATE database_default , [Cd_Societa2] nvarchar(25) COLLATE database_default  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Cd_Storico_Mensile_Rete], [Cd_Pool], [Cd_Societa1], [Cd_Societa2])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Cd_Storico_Mensile_Rete], [Cd_Pool], [Cd_Societa1], [Cd_Societa2]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_New_Relazioni_Societarie]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Cd_Storico_Mensile_Rete], O.[Cd_Pool], O.[Cd_Societa1], O.[Cd_Societa2], O.[id_Tipologia_Societa2]
				FROM
				    [dbo].[Ssh_New_Relazioni_Societarie] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Cd_Storico_Mensile_Rete] = PageIndex.[Cd_Storico_Mensile_Rete]
					AND O.[Cd_Pool] = PageIndex.[Cd_Pool]
					AND O.[Cd_Societa1] = PageIndex.[Cd_Societa1]
					AND O.[Cd_Societa2] = PageIndex.[Cd_Societa2]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_New_Relazioni_Societarie]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_New_Relazioni_Societarie_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_New_Relazioni_Societarie table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdSocieta1" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
				<parameter name="@CdSocieta2" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
				<parameter name="@IdTipologiaSocieta2" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_New_Relazioni_Societarie]
					(
					[Cd_Storico_Mensile_Rete]
					,[Cd_Pool]
					,[Cd_Societa1]
					,[Cd_Societa2]
					,[id_Tipologia_Societa2]
					)
				VALUES
					(
					@CdStoricoMensileRete
					,@CdPool
					,@CdSocieta1
					,@CdSocieta2
					,@IdTipologiaSocieta2
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_New_Relazioni_Societarie_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_New_Relazioni_Societarie table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@OriginalCdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@OriginalCdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdSocieta1" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
				<parameter name="@OriginalCdSocieta1" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
				<parameter name="@CdSocieta2" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
				<parameter name="@OriginalCdSocieta2" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
				<parameter name="@IdTipologiaSocieta2" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_New_Relazioni_Societarie]
				SET
					[Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete
					,[Cd_Pool] = @CdPool
					,[Cd_Societa1] = @CdSocieta1
					,[Cd_Societa2] = @CdSocieta2
					,[id_Tipologia_Societa2] = @IdTipologiaSocieta2
				WHERE
[Cd_Storico_Mensile_Rete] = @OriginalCdStoricoMensileRete 
AND [Cd_Pool] = @OriginalCdPool 
AND [Cd_Societa1] = @OriginalCdSocieta1 
AND [Cd_Societa2] = @OriginalCdSocieta2 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_New_Relazioni_Societarie_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_New_Relazioni_Societarie table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdSocieta1" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
				<parameter name="@CdSocieta2" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_New_Relazioni_Societarie] WITH (ROWLOCK) 
				WHERE
					[Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete
					AND [Cd_Pool] = @CdPool
					AND [Cd_Societa1] = @CdSocieta1
					AND [Cd_Societa2] = @CdSocieta2
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_New_Relazioni_Societarie_GetByIdTipologiaSocieta2" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_New_Relazioni_Societarie table through a foreign key
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@IdTipologiaSocieta2" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SET ANSI_NULLS OFF
				
				SELECT
					[Cd_Storico_Mensile_Rete],
					[Cd_Pool],
					[Cd_Societa1],
					[Cd_Societa2],
					[id_Tipologia_Societa2]
				FROM
					[dbo].[Ssh_New_Relazioni_Societarie]
				WHERE
					[id_Tipologia_Societa2] = @IdTipologiaSocieta2
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_New_Relazioni_Societarie_GetByCdStoricoMensileReteCdPoolCdSocieta1CdSocieta2" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_New_Relazioni_Societarie table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault=""/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault=""/>
				<parameter name="@CdSocieta1" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
				<parameter name="@CdSocieta2" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[Cd_Storico_Mensile_Rete],
					[Cd_Pool],
					[Cd_Societa1],
					[Cd_Societa2],
					[id_Tipologia_Societa2]
				FROM
					[dbo].[Ssh_New_Relazioni_Societarie]
				WHERE
					[Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete
					AND [Cd_Pool] = @CdPool
					AND [Cd_Societa1] = @CdSocieta1
					AND [Cd_Societa2] = @CdSocieta2
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_New_Relazioni_Societarie_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_New_Relazioni_Societarie table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdStoricoMensileRete" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@CdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@CdSocieta1" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault="null"/>
				<parameter name="@CdSocieta2" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault="null"/>
				<parameter name="@IdTipologiaSocieta2" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Cd_Storico_Mensile_Rete]
	, [Cd_Pool]
	, [Cd_Societa1]
	, [Cd_Societa2]
	, [id_Tipologia_Societa2]
    FROM
	[dbo].[Ssh_New_Relazioni_Societarie]
    WHERE 
	 ([Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete OR @CdStoricoMensileRete IS NULL)
	AND ([Cd_Pool] = @CdPool OR @CdPool IS NULL)
	AND ([Cd_Societa1] = @CdSocieta1 OR @CdSocieta1 IS NULL)
	AND ([Cd_Societa2] = @CdSocieta2 OR @CdSocieta2 IS NULL)
	AND ([id_Tipologia_Societa2] = @IdTipologiaSocieta2 OR @IdTipologiaSocieta2 IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Cd_Storico_Mensile_Rete]
	, [Cd_Pool]
	, [Cd_Societa1]
	, [Cd_Societa2]
	, [id_Tipologia_Societa2]
    FROM
	[dbo].[Ssh_New_Relazioni_Societarie]
    WHERE 
	 ([Cd_Storico_Mensile_Rete] = @CdStoricoMensileRete AND @CdStoricoMensileRete is not null)
	OR ([Cd_Pool] = @CdPool AND @CdPool is not null)
	OR ([Cd_Societa1] = @CdSocieta1 AND @CdSocieta1 is not null)
	OR ([Cd_Societa2] = @CdSocieta2 AND @CdSocieta2 is not null)
	OR ([id_Tipologia_Societa2] = @IdTipologiaSocieta2 AND @IdTipologiaSocieta2 is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_New_Relazioni_Societarie_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_New_Relazioni_Societarie table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Cd_Storico_Mensile_Rete] nvarchar(6) COLLATE database_default , [Cd_Pool] nvarchar(20) COLLATE database_default , [Cd_Societa1] nvarchar(25) COLLATE database_default , [Cd_Societa2] nvarchar(25) COLLATE database_default  
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [Cd_Storico_Mensile_Rete], [Cd_Pool], [Cd_Societa1], [Cd_Societa2] )
				SELECT TOP {3} [Cd_Storico_Mensile_Rete], [Cd_Pool], [Cd_Societa1], [Cd_Societa2]
				FROM [dbo].[Ssh_New_Relazioni_Societarie] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[Cd_Storico_Mensile_Rete], O.[Cd_Pool], O.[Cd_Societa1], O.[Cd_Societa2], O.[id_Tipologia_Societa2]
				FROM
				    [dbo].[Ssh_New_Relazioni_Societarie] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[Cd_Storico_Mensile_Rete] = PageIndex.[Cd_Storico_Mensile_Rete]
				    AND O.[Cd_Pool] = PageIndex.[Cd_Pool]
				    AND O.[Cd_Societa1] = PageIndex.[Cd_Societa1]
				    AND O.[Cd_Societa2] = PageIndex.[Cd_Societa2]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_New_Relazioni_Societarie] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		
		<procedure owner="dbo" name="Ssh_SOCIETA_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Ssh_SOCIETA table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_SOCIETA],
					[DS_SOCIETA],
					[TIPOLOGIA_SOCIETA],
					[PIva]
				FROM
					[dbo].[Ssh_SOCIETA]
					
				SELECT @@ROWCOUNT
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="Ssh_SOCIETA_GetPaged" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_SOCIETA table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>
				<parameter name="@PageIndex" type="int" direction="Input" size="0" precision="10" scale="0" />
				<parameter name="@PageSize" type="int" direction="Input" size="0" precision="10" scale="0" /> 
			</parameters>
			<body><![CDATA[
				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_SOCIETA] nvarchar(10) COLLATE database_default , [TIPOLOGIA_SOCIETA] nvarchar(13) COLLATE database_default  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([CD_SOCIETA], [TIPOLOGIA_SOCIETA])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [CD_SOCIETA], [TIPOLOGIA_SOCIETA]'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_SOCIETA]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[CD_SOCIETA], O.[DS_SOCIETA], O.[TIPOLOGIA_SOCIETA], O.[PIva]
				FROM
				    [dbo].[Ssh_SOCIETA] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[CD_SOCIETA] = PageIndex.[CD_SOCIETA]
					AND O.[TIPOLOGIA_SOCIETA] = PageIndex.[TIPOLOGIA_SOCIETA]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Ssh_SOCIETA]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="Ssh_SOCIETA_Insert" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Ssh_SOCIETA table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdSocieta" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@DsSocieta" type="nvarchar" direction="Input" size="100" precision="0" scale="0" param="(100)" nulldefault=""/>
				<parameter name="@TipologiaSocieta" type="nvarchar" direction="Input" size="13" precision="0" scale="0" param="(13)" nulldefault=""/>
				<parameter name="@Piva" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				INSERT INTO [dbo].[Ssh_SOCIETA]
					(
					[CD_SOCIETA]
					,[DS_SOCIETA]
					,[TIPOLOGIA_SOCIETA]
					,[PIva]
					)
				VALUES
					(
					@CdSocieta
					,@DsSocieta
					,@TipologiaSocieta
					,@Piva
					)
				
									
							
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_SOCIETA_Update" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Ssh_SOCIETA table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdSocieta" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@OriginalCdSocieta" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@DsSocieta" type="nvarchar" direction="Input" size="100" precision="0" scale="0" param="(100)" nulldefault=""/>
				<parameter name="@TipologiaSocieta" type="nvarchar" direction="Input" size="13" precision="0" scale="0" param="(13)" nulldefault=""/>
				<parameter name="@OriginalTipologiaSocieta" type="nvarchar" direction="Input" size="13" precision="0" scale="0" param="(13)" nulldefault=""/>
				<parameter name="@Piva" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Ssh_SOCIETA]
				SET
					[CD_SOCIETA] = @CdSocieta
					,[DS_SOCIETA] = @DsSocieta
					,[TIPOLOGIA_SOCIETA] = @TipologiaSocieta
					,[PIva] = @Piva
				WHERE
[CD_SOCIETA] = @OriginalCdSocieta 
AND [TIPOLOGIA_SOCIETA] = @OriginalTipologiaSocieta 
				
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_SOCIETA_Delete" grant="">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Ssh_SOCIETA table
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdSocieta" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@TipologiaSocieta" type="nvarchar" direction="Input" size="13" precision="0" scale="0" param="(13)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				DELETE FROM [dbo].[Ssh_SOCIETA] WITH (ROWLOCK) 
				WHERE
					[CD_SOCIETA] = @CdSocieta
					AND [TIPOLOGIA_SOCIETA] = @TipologiaSocieta
					
			]]></body>
		</procedure>
		<procedure owner="dbo" name="Ssh_SOCIETA_GetByCdSocietaTipologiaSocieta" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Ssh_SOCIETA table through an index
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@CdSocieta" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault=""/>
				<parameter name="@TipologiaSocieta" type="nvarchar" direction="Input" size="13" precision="0" scale="0" param="(13)" nulldefault=""/>
			</parameters>
			<body><![CDATA[
				SELECT
					[CD_SOCIETA],
					[DS_SOCIETA],
					[TIPOLOGIA_SOCIETA],
					[PIva]
				FROM
					[dbo].[Ssh_SOCIETA]
				WHERE
					[CD_SOCIETA] = @CdSocieta
					AND [TIPOLOGIA_SOCIETA] = @TipologiaSocieta
				SELECT @@ROWCOUNT
					
			]]></body>
			
		</procedure>
		<procedure owner="dbo" name="Ssh_SOCIETA_Find" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Ssh_SOCIETA table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@SearchUsingOR" type="bit" direction="Input" size="0" precision="1" scale="0" param="" nulldefault="null"/>
				<parameter name="@CdSocieta" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
				<parameter name="@DsSocieta" type="nvarchar" direction="Input" size="100" precision="0" scale="0" param="(100)" nulldefault="null"/>
				<parameter name="@TipologiaSocieta" type="nvarchar" direction="Input" size="13" precision="0" scale="0" param="(13)" nulldefault="null"/>
				<parameter name="@Piva" type="nvarchar" direction="Input" size="25" precision="0" scale="0" param="(25)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [CD_SOCIETA]
	, [DS_SOCIETA]
	, [TIPOLOGIA_SOCIETA]
	, [PIva]
    FROM
	[dbo].[Ssh_SOCIETA]
    WHERE 
	 ([CD_SOCIETA] = @CdSocieta OR @CdSocieta IS NULL)
	AND ([DS_SOCIETA] = @DsSocieta OR @DsSocieta IS NULL)
	AND ([TIPOLOGIA_SOCIETA] = @TipologiaSocieta OR @TipologiaSocieta IS NULL)
	AND ([PIva] = @Piva OR @Piva IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [CD_SOCIETA]
	, [DS_SOCIETA]
	, [TIPOLOGIA_SOCIETA]
	, [PIva]
    FROM
	[dbo].[Ssh_SOCIETA]
    WHERE 
	 ([CD_SOCIETA] = @CdSocieta AND @CdSocieta is not null)
	OR ([DS_SOCIETA] = @DsSocieta AND @DsSocieta is not null)
	OR ([TIPOLOGIA_SOCIETA] = @TipologiaSocieta AND @TipologiaSocieta is not null)
	OR ([PIva] = @Piva AND @Piva is not null)
	SELECT @@ROWCOUNT			
  END
				]]></body>
		</procedure>

		
		<procedure owner="dbo" name="Ssh_SOCIETA_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Ssh_SOCIETA table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [CD_SOCIETA] nvarchar(10) COLLATE database_default , [TIPOLOGIA_SOCIETA] nvarchar(13) COLLATE database_default  
				)
				
				-- Insert into the temp table
				INSERT INTO #PageIndex ( [CD_SOCIETA], [TIPOLOGIA_SOCIETA] )
				SELECT TOP {3} [CD_SOCIETA], [TIPOLOGIA_SOCIETA]
				FROM [dbo].[Ssh_SOCIETA] {0}
				ORDER BY {1}
				
				-- Return paged results
				SELECT O.[CD_SOCIETA], O.[DS_SOCIETA], O.[TIPOLOGIA_SOCIETA], O.[PIva]
				FROM
				    [dbo].[Ssh_SOCIETA] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > {2}
				    AND O.[CD_SOCIETA] = PageIndex.[CD_SOCIETA]
				    AND O.[TIPOLOGIA_SOCIETA] = PageIndex.[TIPOLOGIA_SOCIETA]
				ORDER BY
				    PageIndex.IndexId
				

				-- get total count
				SELECT COUNT(*) AS TotalRowCount FROM [dbo].[Ssh_SOCIETA] {0};
				
				DROP TABLE #PageIndex
				END
			]]></body>
		</procedure>
		
				
		<procedure owner="dbo" name="UtenzeGasStoricoUpdateAll"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:		
-- Description:	<Update all utenzegas storico in un batch>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdBatch" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@IdState" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
update dbo.Ssh_Utenze_Gas_Storico
set 
Id_Stato =@IdState
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
WHERE
Ssh_Batch_Relazioni.Id_Batch=@IdBatch
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="CleanUpHistory"  skip="true">
			<comment><![CDATA[






-- =============================================
-- Author:	
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	delete  from dbo.Ssh_Utenze_Gas_Storico where
dbo.Ssh_Utenze_Gas_Storico.Id_UtenzeGas_Storico not in                   
(
SELECT MAX(Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico) AS Expr1
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete=@CdMese
GROUP BY Ssh_Utenze_Gas_Storico_2.CD_CLIENTE, Ssh_Utenze_Gas_Storico_2.CD_PUNTO_FORNITURA
)

and Id_UtenzeGas_Storico not in
(
	SELECT Ssh_Utenze_Gas_Storico_2.Id_UtenzeGas_Storico
FROM  Ssh_Utenze_Gas_Storico AS Ssh_Utenze_Gas_Storico_2 INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Utenze_Gas_Storico_2.Id_Trader_Prec_Storico = Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_Batch_Relazioni ON Ssh_Trader_Prec_Storico.Id_Batch_Relazioni = Ssh_Batch_Relazioni.Id_Batch
where Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete<>@CdMese
)
and id_stato not in (2,3)


delete from dbo.Ssh_Trader_Prec_Storico where
Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico not in
(select distinct Id_Trader_Prec_Storico from Ssh_Utenze_Gas_Storico)

Delete from dbo.Ssh_Societa_Storico_Chain
where Id_Batch_Relazioni not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

Delete from dbo.Ssh_Batch_Relazioni where 
id_batch not in
(select distinct id_batch_relazioni from Ssh_Trader_Prec_Storico)

END








			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenze"  skip="true">
			<comment><![CDATA[


-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT Ssh_UTENZE_GAS.CD_MESE_INIZIO_FORNITURA, Ssh_UTENZE_GAS.CD_CLIENTE, Ssh_UTENZE_GAS.DS_CLIENTE, 
               Ssh_UTENZE_GAS.CD_ATECO, Ssh_UTENZE_GAS.CD_PUNTO_FORNITURA, Ssh_UTENZE_GAS.CD_POOL, Ssh_UTENZE_GAS.CD_COMUNE, 
               Ssh_UTENZE_GAS.CONSUMO_ANNUO_STIMATO, Ssh_UTENZE_GAS.CD_PRELIEVO_AEEG, Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE, 
               Ssh_UTENZE_GAS.CD_STATO_PUNTO, Ssh_SOCIETA.DS_SOCIETA
FROM  Ssh_UTENZE_GAS INNER JOIN
               Ssh_SOCIETA ON Ssh_UTENZE_GAS.CD_TRADER_PRECEDENTE = Ssh_SOCIETA.CD_SOCIETA
	where
	CD_POOL=@cdPool and
	CD_MESE_INIZIO_FORNITURA=@cdMese and
	CD_TRADER_PRECEDENTE= @cdTraderPrec 
--and
--	cd_cliente not in
--	(
--		SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
--FROM  Batch_Relazioni INNER JOIN
--               Ssh_Trader_Prec_Storico ON Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
--               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
--		where
--			Ssh_Utenze_Gas_Storico.Id_Stato not in (2,3) and
--			cd_Trader_Precedente=@cdTraderPrec and 
--			Cd_Storico_Mensile_Rete=@cdMese
--	)
order by ds_CLIENTE
END




			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="ConsolidateUsr"  skip="true">
			<comment><![CDATA[

-- =============================================
-- Author:		

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@CdMeseAnno" type="nvarchar" direction="Input" size="50" precision="0" scale="0" param="(50)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update
	Ssh_Utenze_Gas_Storico 
	set Id_Stato=3
	WHERE   (dbo.Ssh_Utenze_Gas_Storico.Id_Stato = 2) 
END



			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="UpdateUtenzeState"  skip="true">
			<comment><![CDATA[
-- =============================================
-- Author:	

-- =============================================
]]></comment>
			<parameters>
				<parameter name="@IdTraderPrecedente" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@NewIdStato" type="int" direction="Input" size="4" precision="10" scale="0" param="" nulldefault="null"/>
				<parameter name="@Nota" type="nvarchar" direction="Input" size="200" precision="0" scale="0" param="(200)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	update dbo.Ssh_Utenze_Gas_Storico
	set Id_Stato=@NewIdStato,
	Note=isnull(@Nota,'')
	where
	Id_Trader_Prec_Storico=@IdTraderPrecedente
	and id_stato not in (2,3)
END

			]]></body>
					</procedure>
				
		<procedure owner="dbo" name="GetUtenzeAll"  skip="true">
			<comment><![CDATA[








-- =============================================


-- =============================================
]]></comment>
			<parameters>
				<parameter name="@cdPool" type="nvarchar" direction="Input" size="20" precision="0" scale="0" param="(20)" nulldefault="null"/>
				<parameter name="@cdMese" type="nvarchar" direction="Input" size="6" precision="0" scale="0" param="(6)" nulldefault="null"/>
				<parameter name="@cdTraderPrec" type="nvarchar" direction="Input" size="10" precision="0" scale="0" param="(10)" nulldefault="null"/>
			</parameters>
			<body><![CDATA[
				

BEGIN
	SELECT UGas.CD_MESE_INIZIO_FORNITURA, UGas.CD_CLIENTE, UGas.DS_CLIENTE, UGas.CD_ATECO, UGas.CD_PUNTO_FORNITURA, UGas.CD_POOL, 
               UGas.CD_COMUNE, UGas.CONSUMO_ANNUO_STIMATO, UGas.CD_PRELIEVO_AEEG, UGas.CD_TRADER_PRECEDENTE, UGas.CD_STATO_PUNTO, 
               0 AS Id_Stato, Comuni.DS_COMUNE, Societa.DS_SOCIETA, UGas.S_AREA_USCITA
FROM  Ssh_UTENZE_GAS AS UGas INNER JOIN
               Ssh_COMUNI AS Comuni ON UGas.CD_COMUNE = Comuni.CD_COMUNE INNER JOIN
               Ssh_SOCIETA AS Societa ON UGas.CD_TRADER_PRECEDENTE = Societa.CD_SOCIETA
WHERE (UGas.CD_POOL = @cdPool) AND (UGas.CD_MESE_INIZIO_FORNITURA = @cdMese) AND 
               (UGas.CD_TRADER_PRECEDENTE = @cdTraderPrec)
				AND (UGas.Cd_Stato_Punto in  ('RICHIESTA_ATT', 'ATTIVATO'))
				AND SOCIETA.TIPOLOGIA_SOCIETA='TRADER' 
			AND (NOT EXISTS
                   (SELECT Ssh_Utenze_Gas_Storico.CD_CLIENTE
                    FROM   Ssh_Batch_Relazioni INNER JOIN
                                   Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
                                   Ssh_Utenze_Gas_Storico ON 
                                   Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico
                    WHERE Ssh_Batch_Relazioni.Cd_Pool=@cdPool
					 AND	(Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec)
					 AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
					 AND (Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA= UGas.CD_PUNTO_FORNITURA)
					 						
))
union
SELECT Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete AS CD_MESE_INIZIO_FORNITURA, Ssh_Utenze_Gas_Storico.CD_CLIENTE, 
               Ssh_Utenze_Gas_Storico.DS_CLIENTE, Ssh_Utenze_Gas_Storico.CD_ATECO, Ssh_Utenze_Gas_Storico.CD_PUNTO_FORNITURA, 
               Ssh_Batch_Relazioni.Cd_Pool, Ssh_Utenze_Gas_Storico.CD_COMUNE, Ssh_Utenze_Gas_Storico.CONSUMO_ANNUO_STIMATO, 
               Ssh_Utenze_Gas_Storico.CD_PRELIEVO_AEEG, Ssh_Trader_Prec_Storico.cd_Trader_Precedente, '' AS cd_stato_punto, 
               Ssh_Utenze_Gas_Storico.Id_Stato, Ssh_COMUNI.DS_COMUNE, Ssh_Trader_Prec_Storico.ragione_sociale AS Ds_Societa, 
               Ssh_Utenze_Gas_Storico.s_area_uscita
FROM  Ssh_Batch_Relazioni INNER JOIN
               Ssh_Trader_Prec_Storico ON Ssh_Batch_Relazioni.Id_Batch = Ssh_Trader_Prec_Storico.Id_Batch_Relazioni INNER JOIN
               Ssh_Utenze_Gas_Storico ON Ssh_Trader_Prec_Storico.Id_Trader_Prec_Storico = Ssh_Utenze_Gas_Storico.Id_Trader_Prec_Storico INNER JOIN
               Ssh_COMUNI ON Ssh_Utenze_Gas_Storico.CD_COMUNE = Ssh_COMUNI.CD_COMUNE
WHERE (Ssh_Batch_Relazioni.Cd_Pool=@cdpool) and (Ssh_Trader_Prec_Storico.cd_Trader_Precedente = @cdTraderPrec) AND (Ssh_Batch_Relazioni.Cd_Storico_Mensile_Rete = @cdMese)
order by ds_CLIENTE
END



			]]></body>
					</procedure>
		<procedure owner="dbo" name="vAllShippers_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vAllShippers view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Cd_Pool],
					[Cd_Storico_Mensile_Rete],
					[DS_SOCIETA],
					[TIPOLOGIA_SOCIETA],
					[CD_SOCIETA],
					[PIva]
				FROM
					[dbo].[vAllShippers]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vAllShippers_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vAllShippers view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vAllShippers]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vAllShippers_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vAllShippers table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vAllShippers] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vBatchTraderPrec_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vBatchTraderPrec view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Id_Batch],
					[Cd_Storico_Mensile_Rete],
					[Cd_Pool],
					[cd_Trader_Precedente],
					[ragione_sociale],
					[CD_CLIENTE],
					[CD_PUNTO_FORNITURA]
				FROM
					[dbo].[vBatchTraderPrec]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vBatchTraderPrec_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vBatchTraderPrec view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vBatchTraderPrec]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vBatchTraderPrec_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vBatchTraderPrec table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vBatchTraderPrec] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vBatchTraderPrecSt_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vBatchTraderPrecSt view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Id_Trader_Prec_Storico],
					[piva],
					[Cd_Storico_Mensile_Rete],
					[id_tipologia],
					[Cd_Pool],
					[DsSocieta],
					[CdSocieta],
					[dt_ins],
					[CdShipper],
					[DsShipper],
					[Id_Batch],
					[LTrader]
				FROM
					[dbo].[vBatchTraderPrecSt]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vBatchTraderPrecSt_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vBatchTraderPrecSt view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vBatchTraderPrecSt]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vBatchTraderPrecSt_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vBatchTraderPrecSt table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vBatchTraderPrecSt] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vBatchTraderPrecStPool_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vBatchTraderPrecStPool view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Id_Trader_Prec_Storico],
					[piva],
					[Cd_Storico_Mensile_Rete],
					[id_tipologia],
					[Cd_Pool],
					[DsSocieta],
					[CdSocieta],
					[dt_ins],
					[CdShipper],
					[DsShipper],
					[Id_Batch]
				FROM
					[dbo].[vBatchTraderPrecStPool]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vBatchTraderPrecStPool_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vBatchTraderPrecStPool view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vBatchTraderPrecStPool]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vBatchTraderPrecStPool_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vBatchTraderPrecStPool table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vBatchTraderPrecStPool] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vBatchUtenze_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vBatchUtenze view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Id_Batch],
					[Cd_Storico_Mensile_Rete],
					[Cd_Pool],
					[Id_Trader_Prec_Storico],
					[piva],
					[ragione_sociale],
					[Id_UtenzeGas_Storico],
					[Expr1],
					[Id_Batch_Relazioni],
					[CD_MESE_INIZIO_FORNITURA],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[Id_Stato],
					[id_tipologia],
					[CD_COMUNE],
					[DS_COMUNE],
					[dt_ins],
					[dt_upd],
					[linearizedTPrec]
				FROM
					[dbo].[vBatchUtenze]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vBatchUtenze_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vBatchUtenze view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vBatchUtenze]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vBatchUtenze_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vBatchUtenze table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vBatchUtenze] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vCdMese_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vCdMese view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_MESE_INIZIO_FORNITURA],
					[TxMese]
				FROM
					[dbo].[vCdMese]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vCdMese_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vCdMese view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vCdMese]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vCdMese_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vCdMese table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vCdMese] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vCdMeseAll_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vCdMeseAll view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_MESE_INIZIO_FORNITURA],
					[TxMese]
				FROM
					[dbo].[vCdMeseAll]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vCdMeseAll_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vCdMeseAll view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vCdMeseAll]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vCdMeseAll_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vCdMeseAll table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vCdMeseAll] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vConsolidaUtenze_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vConsolidaUtenze view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_CLIENTE],
					[DS_CLIENTE],
					[DS_COMUNE],
					[CD_PUNTO_FORNITURA],
					[Id_Stato_Lavorazione_utenza],
					[cd_Trader_Precedente],
					[Cd_Pool],
					[Cd_Storico_Mensile_Rete],
					[CD_COMUNE],
					[Ragione_Sociale],
					[cd_societa]
				FROM
					[dbo].[vConsolidaUtenze]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vConsolidaUtenze_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vConsolidaUtenze view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vConsolidaUtenze]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vConsolidaUtenze_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vConsolidaUtenze table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vConsolidaUtenze] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vIntersecRelazioniSocietarie_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vIntersecRelazioniSocietarie view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_SOCIETA1],
					[CdSocieta],
					[CD_POOL],
					[CD_STORICO_MENSILE_RETE],
					[DS_SOCIETA],
					[TIPOLOGIA_SOCIETA]
				FROM
					[dbo].[vIntersecRelazioniSocietarie]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vIntersecRelazioniSocietarie_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vIntersecRelazioniSocietarie view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vIntersecRelazioniSocietarie]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vIntersecRelazioniSocietarie_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vIntersecRelazioniSocietarie table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vIntersecRelazioniSocietarie] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vIntersecShipperAttiviPool_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vIntersecShipperAttiviPool view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_SOCIETA],
					[DS_SOCIETA],
					[TIPOLOGIA_SOCIETA],
					[CD_POOL],
					[CD_STORICO_MENSILE_RETE],
					[PIva]
				FROM
					[dbo].[vIntersecShipperAttiviPool]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vIntersecShipperAttiviPool_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vIntersecShipperAttiviPool view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vIntersecShipperAttiviPool]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vIntersecShipperAttiviPool_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vIntersecShipperAttiviPool table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vIntersecShipperAttiviPool] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vIntersecShipperFilter_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vIntersecShipperFilter view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[cd_societa],
					[Ds_Societa],
					[Cd_Pool],
					[Cd_Storico_Mensile_Rete],
					[Tipologia_Societa],
					[cd_Trader_Precedente]
				FROM
					[dbo].[vIntersecShipperFilter]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vIntersecShipperFilter_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vIntersecShipperFilter view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vIntersecShipperFilter]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vIntersecShipperFilter_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vIntersecShipperFilter table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vIntersecShipperFilter] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vNewRelazioniSocietarie_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vNewRelazioniSocietarie view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Cd_Societa1],
					[Cd_Societa2],
					[Cd_Pool],
					[Cd_Storico_Mensile_Rete],
					[DS_SOCIETA],
					[TIPOLOGIA_SOCIETA],
					[PIva]
				FROM
					[dbo].[vNewRelazioniSocietarie]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vNewRelazioniSocietarie_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vNewRelazioniSocietarie view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vNewRelazioniSocietarie]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vNewRelazioniSocietarie_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vNewRelazioniSocietarie table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vNewRelazioniSocietarie] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vNewSocieta_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vNewSocieta view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Piva],
					[Ds_Societa],
					[Tipologia_Societa],
					[Cd_Societa],
					[Id_Tipologia_Societa],
					[Cd_Storico_Mensile_Rete]
				FROM
					[dbo].[vNewSocieta]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vNewSocieta_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vNewSocieta view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vNewSocieta]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vNewSocieta_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vNewSocieta table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vNewSocieta] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vPool_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vPool view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_POOL],
					[CD_SHIPPER_DEFAULT],
					[CD_COMUNE],
					[CD_MESE_INIZIO_FORNITURA],
					[Id_Stato]
				FROM
					[dbo].[vPool]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vPool_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vPool view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vPool]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vPool_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vPool table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vPool] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vRejectedShippers_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vRejectedShippers view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[cd_societa],
					[Id_Tipologia_Societa],
					[Id_Stato],
					[id_societa],
					[Ragione_Sociale],
					[Cd_Storico_Mensile_Rete],
					[Cd_Pool],
					[cd_Trader_Precedente]
				FROM
					[dbo].[vRejectedShippers]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vRejectedShippers_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vRejectedShippers view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vRejectedShippers]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vRejectedShippers_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vRejectedShippers table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vRejectedShippers] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vRelazioniSocietarie_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vRelazioniSocietarie view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_SOCIETA1],
					[CdSocieta],
					[CD_POOL],
					[CD_STORICO_MENSILE_RETE],
					[DS_SOCIETA],
					[TIPOLOGIA_SOCIETA]
				FROM
					[dbo].[vRelazioniSocietarie]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vRelazioniSocietarie_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vRelazioniSocietarie view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vRelazioniSocietarie]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vRelazioniSocietarie_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vRelazioniSocietarie table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vRelazioniSocietarie] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vReport_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vReport view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[DS_COMUNE],
					[DsTrasportatore],
					[CD_CLIENTE],
					[Dsc_Stato],
					[AreaUscita],
					[CD_STORICO_MENSILE_RETE],
					[CD_PRELIEVO_AEEG],
					[S_Area_Uscita],
					[CONSUMO_ANNUO_STIMATO],
					[Cd_Pool],
					[Ds_Societa],
					[CD_PUNTO_FORNITURA],
					[DS_CLIENTE],
					[DsShipper]
				FROM
					[dbo].[vReport]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vReport_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vReport view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vReport]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vReport_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vReport table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vReport] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vShipperAttiviPool_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vShipperAttiviPool view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_POOL],
					[CD_STORICO_MENSILE_RETE],
					[piva],
					[CD_SOCIETA],
					[DS_SOCIETA],
					[TIPOLOGIA_SOCIETA]
				FROM
					[dbo].[vShipperAttiviPool]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vShipperAttiviPool_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vShipperAttiviPool view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vShipperAttiviPool]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vShipperAttiviPool_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vShipperAttiviPool table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vShipperAttiviPool] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vShipperDefaultPool_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vShipperDefaultPool view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_SOCIETA],
					[DS_SOCIETA],
					[TIPOLOGIA_SOCIETA],
					[CD_POOL],
					[CD_STORICO_MENSILE_RETE],
					[PIva]
				FROM
					[dbo].[vShipperDefaultPool]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vShipperDefaultPool_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vShipperDefaultPool view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vShipperDefaultPool]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vShipperDefaultPool_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vShipperDefaultPool table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vShipperDefaultPool] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vSimpleShipperDefaultPool_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vSimpleShipperDefaultPool view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_SOCIETA],
					[DS_SOCIETA],
					[TIPOLOGIA_SOCIETA],
					[CD_POOL],
					[CD_STORICO_MENSILE_RETE],
					[PIva]
				FROM
					[dbo].[vSimpleShipperDefaultPool]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vSimpleShipperDefaultPool_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vSimpleShipperDefaultPool view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vSimpleShipperDefaultPool]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vSimpleShipperDefaultPool_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vSimpleShipperDefaultPool table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vSimpleShipperDefaultPool] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vSimpleShipperFilter_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vSimpleShipperFilter view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[cd_societa],
					[Ds_Societa],
					[Cd_Pool],
					[Cd_Storico_Mensile_Rete],
					[Tipologia_Societa],
					[cd_Trader_Precedente]
				FROM
					[dbo].[vSimpleShipperFilter]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vSimpleShipperFilter_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vSimpleShipperFilter view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vSimpleShipperFilter]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vSimpleShipperFilter_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vSimpleShipperFilter table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vSimpleShipperFilter] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vSimpleTraderPrec_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vSimpleTraderPrec view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_POOL],
					[CD_SOCIETA],
					[DS_SOCIETA],
					[CD_STORICO_MENSILE_RETE],
					[CD_COMUNE]
				FROM
					[dbo].[vSimpleTraderPrec]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vSimpleTraderPrec_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vSimpleTraderPrec view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vSimpleTraderPrec]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vSimpleTraderPrec_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vSimpleTraderPrec table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vSimpleTraderPrec] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vSocietaAll_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vSocietaAll view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_SOCIETA],
					[PIva],
					[DS_SOCIETA],
					[TIPOLOGIA_SOCIETA],
					[Mese_Inizio_Fornitura],
					[Ds_Societa_Full]
				FROM
					[dbo].[vSocietaAll]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vSocietaAll_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vSocietaAll view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vSocietaAll]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vSocietaAll_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vSocietaAll table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vSocietaAll] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vSocietaStorico_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vSocietaStorico view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Cd_Storico_Mensile_Rete],
					[cd_societa],
					[Cd_Pool],
					[PIva],
					[Tipologia_Societa],
					[Ds_Societa],
					[cd_Trader_Precedente]
				FROM
					[dbo].[vSocietaStorico]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vSocietaStorico_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vSocietaStorico view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vSocietaStorico]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vSocietaStorico_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vSocietaStorico table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vSocietaStorico] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTpPool_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vTpPool view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_POOL],
					[CD_TRADER_PRECEDENTE],
					[CD_MESE_INIZIO_FORNITURA]
				FROM
					[dbo].[vTpPool]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTpPool_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTpPool view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vTpPool]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vTpPool_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTpPool table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vTpPool] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTPUtenze_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vTPUtenze view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_CLIENTE],
					[DS_CLIENTE],
					[DS_COMUNE],
					[CD_PUNTO_FORNITURA],
					[Id_Stato_Lavorazione_utenza],
					[cd_Trader_Precedente],
					[Cd_Pool],
					[Cd_Storico_Mensile_Rete],
					[CD_COMUNE],
					[CD_ATECO],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[ragione_sociale]
				FROM
					[dbo].[vTPUtenze]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTPUtenze_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTPUtenze view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vTPUtenze]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vTPUtenze_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTPUtenze table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vTPUtenze] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTpUtenzeAll_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vTpUtenzeAll view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_CLIENTE],
					[DS_CLIENTE],
					[DS_COMUNE],
					[CD_PUNTO_FORNITURA],
					[CD_TRADER_PRECEDENTE],
					[CD_POOL],
					[cd_storico_mensile_rete],
					[CD_COMUNE],
					[CD_ATECO],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[ragione_sociale],
					[IdStato],
					[Expr1],
					[Expr2],
					[S_AREA_USCITA]
				FROM
					[dbo].[vTpUtenzeAll]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTpUtenzeAll_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTpUtenzeAll view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vTpUtenzeAll]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vTpUtenzeAll_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTpUtenzeAll table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vTpUtenzeAll] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTputenzePool_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vTputenzePool view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_CLIENTE],
					[DS_CLIENTE],
					[DS_COMUNE],
					[CD_PUNTO_FORNITURA],
					[Id_Stato_Lavorazione_utenza],
					[CD_TRADER_PRECEDENTE],
					[CD_POOL],
					[Cd_Storico_Mensile_Rete],
					[CD_COMUNE],
					[CD_ATECO],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[DS_SOCIETA],
					[CD_SOCIETA],
					[TIPOLOGIA_SOCIETA],
					[PIva],
					[S_AREA_USCITA]
				FROM
					[dbo].[vTputenzePool]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTputenzePool_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTputenzePool view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vTputenzePool]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vTputenzePool_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTputenzePool table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vTputenzePool] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTPUtenzeStorico_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vTPUtenzeStorico view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_CLIENTE],
					[DS_CLIENTE],
					[DS_COMUNE],
					[CD_PUNTO_FORNITURA],
					[Id_Stato_Lavorazione_utenza],
					[cd_Trader_Precedente],
					[Cd_Pool],
					[Cd_Storico_Mensile_Rete],
					[CD_COMUNE],
					[CD_ATECO],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[ragione_sociale],
					[Id_Trader_Prec_Storico],
					[Id_UtenzeGas_Storico],
					[Id_Batch]
				FROM
					[dbo].[vTPUtenzeStorico]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTPUtenzeStorico_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTPUtenzeStorico view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vTPUtenzeStorico]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vTPUtenzeStorico_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTPUtenzeStorico table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vTPUtenzeStorico] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTraderInRelationship_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vTraderInRelationship view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[Id_Batch],
					[Cd_Storico_Mensile_Rete],
					[dt_ins],
					[Cd_Pool],
					[cd_societa],
					[Id_Tipologia_Societa]
				FROM
					[dbo].[vTraderInRelationship]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTraderInRelationship_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTraderInRelationship view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vTraderInRelationship]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vTraderInRelationship_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTraderInRelationship table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vTraderInRelationship] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTradersPrec_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vTradersPrec view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_SOCIETA],
					[DS_SOCIETA],
					[TIPOLOGIA_SOCIETA],
					[CD_MESE_INIZIO_FORNITURA],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[CD_ATECO],
					[CD_PUNTO_FORNITURA],
					[CD_COMUNE],
					[CD_STATO_PUNTO],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[CD_TRADER_PRECEDENTE],
					[CD_TRASPORTATORE],
					[piva],
					[dt_ins],
					[Pool_Lavorazione],
					[CD_POOL],
					[DS_COMUNE],
					[Id_Stato_Lavorazione_Utenza],
					[Ds_Client_Storico]
				FROM
					[dbo].[vTradersPrec]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vTradersPrec_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTradersPrec view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vTradersPrec]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vTradersPrec_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vTradersPrec table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vTradersPrec] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vUtenzeInPool_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vUtenzeInPool view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_MESE_INIZIO_FORNITURA],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[CD_ATECO]
				FROM
					[dbo].[vUtenzeInPool]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vUtenzeInPool_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vUtenzeInPool view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vUtenzeInPool]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vUtenzeInPool_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vUtenzeInPool table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vUtenzeInPool] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vUtenzeNotInPool_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vUtenzeNotInPool view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_MESE_INIZIO_FORNITURA],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[CD_ATECO],
					[CD_PUNTO_FORNITURA],
					[CD_POOL],
					[CD_COMUNE],
					[CONSUMO_ANNUO_STIMATO],
					[CD_PRELIEVO_AEEG],
					[CD_TRADER_PRECEDENTE],
					[CD_STATO_PUNTO],
					[DS_COMUNE]
				FROM
					[dbo].[vUtenzeNotInPool]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vUtenzeNotInPool_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vUtenzeNotInPool view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vUtenzeNotInPool]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vUtenzeNotInPool_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vUtenzeNotInPool table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vUtenzeNotInPool] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vUtenzePoolNonDefinito_Get_List" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the vUtenzePoolNonDefinito view
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				SELECT
					[CD_MESE_INIZIO_FORNITURA],
					[CD_CLIENTE],
					[DS_CLIENTE],
					[CD_PUNTO_FORNITURA],
					[CD_TRADER_PRECEDENTE],
					[CD_POOL],
					[DS_COMUNE]
				FROM
					[dbo].[vUtenzePoolNonDefinito]
					
				SELECT @@ROWCOUNT			
			]]></body>
		</procedure>
		
		<procedure owner="dbo" name="vUtenzePoolNonDefinito_Get" >
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vUtenzePoolNonDefinito view passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters>
				<parameter name="@WhereClause" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)" />
				<parameter name="@OrderBy" type="varchar" direction="Input" size="2000" precision="0" scale="0" param="(2000)"/>			
			</parameters>
			<body><![CDATA[
				
				BEGIN

				-- Build the sql query
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = ' SELECT * FROM [dbo].[vUtenzePoolNonDefinito]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Execution the query
				EXEC sp_executesql @SQL
				
				-- Return total count
				SELECT @@ROWCOUNT AS TotalRowCount
				
				END
			]]></body>
		</procedure>
		
		
		<procedure owner="dbo" name="vUtenzePoolNonDefinito_Find_Dynamic"  skip="true">
			<comment><![CDATA[/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the vUtenzePoolNonDefinito table using a dynamically generated query.
----------------------------------------------------------------------------------------------------
*/
]]></comment>
			<parameters/>
			<body><![CDATA[
				
				BEGIN
				
				SELECT * FROM [dbo].[vUtenzePoolNonDefinito] {0}
				ORDER BY {1}

				-- get total count
				SELECT @@ROWCOUNT AS TotalRowCount;
				
				END
			]]></body>
		</procedure>
		
</procedures>
</root>
